0
dataframe %>%
  select(thumb_p1_frac_1, thumb_p1_frac_2, thumb_p1_frac_3, thumb_p1_frac_4, thumb_p1_frac_5) %>%
  mutate(P1 = rowSums(., na.rm = TRUE)) 

above works, but I want to remove the pipe of select and implement select select in mutate, like

dataframe %>%
  mutate(P1 = rowSums(., select(thumb_p1_frac_1, thumb_p1_frac_2, thumb_p1_frac_3, thumb_p1_frac_4, thumb_p1_frac_5), na.rm = TRUE))  

it says:

no applicable method for 'select_' applied to an object of class "c('double', 'numeric')"

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

0

As your error said, you choose the wrong input to the select() function. The reason for this is that select() takes a data.frame but you only supply separate columns! So a solution to your problem is to give the select() command the . which represents the data.frame that you pipe through the %>%.

Hence, a correct code is

dataframe %>%
  mutate(P1 = rowSums(select(., thumb_p1_frac_1, thumb_p1_frac_2, 
                              thumb_p1_frac_3, thumb_p1_frac_4, thumb_p1_frac_5), 
                na.rm = TRUE))

Note that I pulled out the . from within rowSums() and instead used it in select().

Taufi
  • 1,557
  • 8
  • 14