Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine transmute with grep function?

Tags:

r

dplyr

I'm trying to find a way to create a new table with variables using the rowSums() function from an existing dataframe. For example, my existing dataframe is called 'asn' and I want to sum up the values for each row of all variables which contain "2011" in the variable title. I want a new table consisting of just one column called asn_y2011 which contains the sum of each row using the variables containing "2011"

Data

structure(list(row = 1:3, south_2010 = c(1L, 5L, 7L), south_2011 = c(4L, 
0L, 4L), south_2012 = c(5L, 8L, 6L), north_2010 = c(3L, 4L, 1L
), north_2011 = c(2L, 6L, 0L), north_2012 = c(1L, 1L, 2L)), class = "data.frame", row.names = c(NA, 
-3L))

The existing 'asn' dataframe looks like this

row south_2010 south_2011 south_2012 north_2010 north_2011 north_2012
  1      1           4         5          3          2          1
  2      5           0         8          4          6          1
  3      7           4         6          1          0          2

I'm trying to use the following function:

asn %>%   
   transmute(asn_y2011 = rowSums(, grep("2011")))

to get something like this

row    asn_y2011
 1         6
 2         6
 3         4
like image 420
Adam Avatar asked Jan 24 '23 20:01

Adam


2 Answers

Continuing with your code, grep() should work like this:

library(dplyr)

asn %>%
  transmute(row, asn_y2011 = rowSums(.[grep("2011", names(.))]))

#   row asn_y2011
# 1   1         6
# 2   2         6
# 3   3         4

Or you can use tidy selection in c_across():

asn %>%
  rowwise() %>% 
  transmute(row, asn_y2011 = sum(c_across(contains("2011")))) %>%
  ungroup()
like image 121
Darren Tsai Avatar answered Jan 30 '23 01:01

Darren Tsai


Another base R option using rowSums

cbind(asn[1],asn_y2011 = rowSums(asn[grep("2011",names(asn))]))

which gives

  row asn_y2011
1   1         6
2   2         6
3   3         4
like image 43
ThomasIsCoding Avatar answered Jan 30 '23 00:01

ThomasIsCoding