-1

Given a dataframe (my_data) in R such as the following

category  Keyword1 Keyword2 Keyword3 Keyword4 Keyword5 Keyword6 Keyword7 Keyword8
123         0        1         1       0         0        0       0         1
155         1        0         0       0         1        0       1         1
144         0        0         1       0         0        0       1         1
123         1        1         0       0         0        0       1         1

I want to transform the dataframe by taking rows with category id values that already exist (e.g category 123) and combine them. The result should look like:

category Keyword1 Keyword2 Keyword3 Keyword4 Keyword5 Keyword6 Keyword7 Keyword8
123         1        1         1       0         0        0       0         1
155         1        0         0       0         1        0       1         1
144         0        0         1       0         0        0       1         1

How can I do this in R ?

Yamane Imad
  • 43
  • 1
  • 6

1 Answers1

2

You can use dplyr, which is useful for many other such use cases as follows:

library(dplyr)
my_data %>% group_by(category) %>% summarise_each(funs(max)) 

Output is:

# A tibble: 3 × 9
  category Keyword1 Keyword2 Keyword3 Keyword4 Keyword5 Keyword6 Keyword7 Keyword8
     <int>    <int>    <int>    <int>    <int>    <int>    <int>    <int>    <int>
1      123        1        1        1        0        0        0        1        1
2      144        0        0        1        0        0        0        1        1
3      155        1        0        0        0        1        0        1        1
Gopala
  • 10,363
  • 7
  • 45
  • 77
  • Is it possible to get both the Keyword1 value of rows with same category as "0, 1"? This is an alternative of doing a summation like above. Thank you so much. – emeralddove Dec 06 '17 at 10:15
  • @Gopala this is what i was looking for my microbiome data and it works perfectly fine thanks a ton – PesKchan Nov 30 '22 at 18:03