Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing a range of columns in dplyr

Tags:

r

filter

dplyr

sum

let say I have a data frame df like that

    txt    A1    A2    B1    B2
1   ala    6      9    12    23
2   ata    1      3    3     11
....

I would like to use dplyr for filtering the rows based on a sum of a range of variables. I tried:

filter(df, sum(A2:B1)>10)

.... but it does not work.

Could anyone suggest a solution in dplyr? And yes I know it can be done differently by simple subsetting.

like image 818
kwicher Avatar asked Jun 10 '16 13:06

kwicher


People also ask

How do I specify multiple columns in R?

To get multiple columns of matrix, specify the column numbers as a vector preceded by a comma, in square brackets, after the matrix variable name. This expression returns the required columns as a matrix.

What does %>% do in dplyr?

%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression. It is defined by the package magrittr (CRAN) and is heavily used by dplyr (CRAN).

How do I show specific columns in R?

To select a column in R you can use brackets e.g., YourDataFrame['Column'] will take the column named “Column”. Furthermore, we can also use dplyr and the select() function to get columns by name or index. For instance, select(YourDataFrame, c('A', 'B') will take the columns named “A” and “B” from the dataframe.


2 Answers

I think the most dplyr-esque way would be:

df %>%
  filter(rowSums(select_(., 'A2:B1')) > 10)

Which gives:

#  txt A1 A2 B1 B2
#1 ala  6  9 12 23
like image 84
Steven Beaupré Avatar answered Oct 11 '22 00:10

Steven Beaupré


We can get the indexes first and then use rowSums,

v1 <- which(names(df) == 'A2') #find first column
#[1] 3
v2 <- which(names(df) == 'B1') #find last column
#[1] 4
df[rowSums(df[v1:v2])>10,]
#  txt A1 A2 B1 B2
#1 ala  6  9 12 23
like image 36
Sotos Avatar answered Oct 11 '22 00:10

Sotos