[R] pasting several things

G See gsee000 at gmail.com
Sat Mar 3 03:00:33 CET 2012


Try this:

x <- structure(list(day = 19, C1 = structure(1L, .Label = c("", "C1"
), class = "factor"), C2 = structure(2L, .Label = c("", "C2"), class =
"factor"),
   C3 = structure(1L, .Label = c("", "C3"), class = "factor"),
   Q1 = structure(2L, .Label = c("", "Q1"), class = "factor"),
   Q2 = structure(2L, .Label = c("", "Q2"), class = "factor"),
   Q3 = structure(1L, .Label = c("", "Q3"), class = "factor")), .Names =
c("day",
"C1", "C2", "C3", "Q1", "Q2", "Q3"), row.names = "8", class = "data.frame")

paste(x[1, 1], do.call(paste, c(x[1, x != ""][, -1], list(sep="_"))), sep=" -")

# Output looks like this
> paste(x[1, 1], do.call(paste, c(x[1, x != ""][, -1], list(sep="_"))), sep=" -")
[1] "19 -C2_Q1_Q2"
> x[1, 2] <- "C1"
> paste(x[1, 1], do.call(paste, c(x[1, x != ""][, -1], list(sep="_"))), sep=" -")
[1] "19 -C1_C2_Q1_Q2"

HTH,
Garrett

On Fri, Mar 2, 2012 at 2:39 PM, chuck.01 <CharlieTheBrown77 at gmail.com> wrote:
> I have this type of format:
>
> structure(list(day = 19, C1 = structure(1L, .Label = c("", "C1"
> ), class = "factor"), C2 = structure(2L, .Label = c("", "C2"), class =
> "factor"),
>    C3 = structure(1L, .Label = c("", "C3"), class = "factor"),
>    Q1 = structure(2L, .Label = c("", "Q1"), class = "factor"),
>    Q2 = structure(2L, .Label = c("", "Q2"), class = "factor"),
>    Q3 = structure(1L, .Label = c("", "Q3"), class = "factor")), .Names =
> c("day",
> "C1", "C2", "C3", "Q1", "Q2", "Q3"), row.names = "8", class = "data.frame")
>>
>
> and want something like this:
>
>  19 -C2 _Q1_Q2
>
> any ideas?  obviously I could use paste() and get this.  Keep in mind I have
> many of these and the presence of "C1", "C2", ... etc will vary.
>
>
> Thanks.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/pasting-several-things-tp4439770p4439770.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list