[R] Merge two columns of a data frame

Ethan Brown ethancbrown at gmail.com
Mon Jun 6 22:46:05 CEST 2011


Another possibility:

dfs <- list(df1, df2, df3)
df.1.2.3 <- as.data.frame(unlist(sapply(dfs, function(x) do.call(paste, x))))

On Mon, Jun 6, 2011 at 2:37 PM, Ista Zahn <izahn at psych.rochester.edu> wrote:
> Hi Abraham,
> Just take it step by step. Paste the values together, combine them,
> and assign them to a data.frame column. Like this perhaps:
>
> df.1.2.3 <- data.frame(Var1 =
>        c(with(df1, paste(Var1, Var2, Var3)),
>          with(df2, paste(Var1, Var2)),
>          with(df3, paste(Var1, Var2))))
>
> Best,
> Ista
>
> On Mon, Jun 6, 2011 at 12:22 PM, Abraham Mathew <abraham at thisorthat.com> wrote:
>> I have the following data:
>>
>> prefix <- c("cheap", "budget")
>> roots <- c("car insurance", "auto insurance")
>> suffix <- c("quote", "quotes")
>>
>> prefix2 <- c("cheap", "budget")
>> roots2 <- c("car insurance", "auto insurance")
>>
>> roots3 <- c("car insurance", "auto insurance")
>> suffix3 <- c("quote", "quotes")
>>
>> df1 <- expand.grid(prefix, roots, suffix)
>> df2 <- expand.grid(prefix2, roots2)
>> df3 <- expand.grid(roots3, suffix3)
>> df1; df2; df3
>>
>> df1, df2, and df3 are seperate data structures with seperate columns for
>> root, prefix, and suffix.
>>
>>  Var1           Var2   Var3
>> 1  cheap  car insurance  quote
>> 2 budget  car insurance  quote
>> 3  cheap auto insurance  quote
>> 4 budget auto insurance  quote
>> 5  cheap  car insurance quotes
>> 6 budget  car insurance quotes
>> 7  cheap auto insurance quotes
>> 8 budget auto insurance quotes
>>    Var1           Var2
>> 1  cheap  car insurance
>> 2 budget  car insurance
>> 3  cheap auto insurance
>> 4 budget auto insurance
>>            Var1   Var2
>> 1  car insurance  quote
>> 2 auto insurance  quote
>> 3  car insurance quotes
>> 4 auto insurance quotes
>>
>>
>> I want to merge df1, df2, and df3, into one data frame column which looks
>> like.
>>
>>                    Var1
>>  'cheap  car insurance  quote'
>>  'budget  car insurance  quote'
>>  'cheap auto insurance  quote'
>>  'budget auto insurance  quote'
>>  'cheap  car insurance quotes'
>>  'budget  car insurance quotes'
>>  'cheap auto insurance quotes'
>>  'budget auto insurance quotes'
>>         'cheap  car insurance'
>>         'budget  car insurance'
>>        'cheap auto insurance'
>>        'budget auto insurance'
>>        'car insurance  quote'
>>         'auto insurance  quote'
>>        'car insurance quotes'
>>        'auto insurance quotes'
>>
>>
>> Help!
>> WebRep
>> Overall rating
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Ista Zahn
> Graduate student
> University of Rochester
> Department of Clinical and Social Psychology
> http://yourpsyche.org
>
> ______________________________________________
> 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