[Solved] Extract rows based on only one column using R [closed]


Tried to create your data using the following:

df <- data.frame(col1 = c(1,1,2,2,1,1,2,2),
                 col2 = c(34,34,342,23,34,34,342,23),
                 col3 = c(3,4,3,4,3,4,3,4))

And, if you wish to subset based on only one column, you can use @kyle-marsh solution

> df[df$col1 == 1, ]
  col1 col2 col3
1    1   34    3
2    1   34    4
5    1   34    3
6    1   34    4

And, if you need only distinct columns, maybe you can try this

> unique(df[df$col1 == 1, ])
  col1 col2 col3
1    1   34    3
2    1   34    4

And, if you need to aggregate the data based on col1, you can try the following. Since, col1 is numeric,

> aggregate(df[,-1], by = list(col1 = df$col1), sum)
  col1 col2 col3
1    1  136   14
2    2  730   14

0

solved Extract rows based on only one column using R [closed]