Aleksandr Ianevski
Aleksandr Ianevski

Reputation: 1944

Merge dataframe rows by the same names

Having the following dataframe as an example:

data_ = data.frame(name = c("name1", "name2","name1"), product = c("product1", "product2", "product3"))

The output is:

name1 product1
name2 product2
name1 product3

I would like to merge it rows by the same names. Therefore, the result would look like:

name1 product1, product3
name2 product2

I am using R 2.15.2, so I can't use aggregate

Upvotes: 0

Views: 83

Answers (1)

lmo
lmo

Reputation: 38500

Perhaps the most natural way to store this data is as a list, because there will be different numbers of elements per name. Here is a way to get a named list with character vectors for your products:

lapply(split(data_, data_$name), function(i) {i$name <- NULL; as.character(unlist(i))})
$name1
[1] "product1" "product3"

$name2
[1] "product2"

I converted the products to characters which are typically easier to work with using as.character, though that is not necessary.

In the original example, the object data_ was a data frame. However, if it is a list, then the code becomes event simpler:

# build list
data_ = list(name = c("name1", "name2","name1"), product = c("product1", "product2", "product3"))

# split list along names variable:
split(data_$product, data_$name)
$name1
[1] "product1" "product3"

$name2
[1] "product2"

Which is the same as above, but we didn't have to unload the extra structure than accompanies splitting a data frame with a factor variable.

Upvotes: 3

Related Questions