Home > Blockchain >  Rowwise summing factor columns in dataframe
Rowwise summing factor columns in dataframe

Time:11-01

I have a tibble called ehp30 with 13 columns which correspond to the answers of a questionnaire (11 columns) by patients at a certain followup timepoint. First row is (with fake data):

   Pat_TNO AssNo    eSocialEvents eJobsAtHome eStandingUp eSittingDown eWalking  eExercise eAppetite eSleep    eHadToLieDown eUnableToDo eUnableToCope
     <dbl> <fct>    <fct>         <fct>       <fct>       <fct>        <fct>     <fct>     <fct>     <fct>     <fct>         <fct>       <fct>        
 1    1234 baseline Often         Often       Sometimes   Sometimes    Sometimes Often     Sometimes Often     Often         Often       Sometimes    

I want to mutate a new column on the end called 'pain' which is the sum of the values from the 11 columns between eSocialEvents and eUnableToCope. The levels are:

 "Never"     "Rarely"    "Sometimes" "Often"     "Always"   

which are effectively numbered 1-5, so the sum of the first row should be 39.

CodePudding user response:

You can convert to numeric, and then use rowSums:

library(dplyr)
dat %>%
  mutate(across(starts_with("e"), as.numeric),
         rowS = rowSums(select(., starts_with("e")))

CodePudding user response:

With some sample data in a reproducible format

dd <- read.table(text="Pat_TNO AssNo    eSocialEvents eJobsAtHome eStandingUp eSittingDown eWalking  eExercise eAppetite eSleep    eHadToLieDown eUnableToDo eUnableToCope
  1    1234 baseline Often         Often       Sometimes   Sometimes    Sometimes Often     Sometimes Often     Often         Often       Sometimes   
  2    4567 baseline Always        Often       Sometimes   Sometimes    Sometimes Often     Sometimes Often     Often         Often       Sometimes", header=TRUE)

You can use dplyr to sum the rows with the given level order

levels <- c("Never", "Rarely", "Sometimes", "Often", "Always")

dd %>% 
  rowwise() %>% 
  mutate(score=sum(match(c_across(starts_with("e")), levels))) %>% 
  select(AssNo, score)

Here we use match() to look up the index of the value in the levels vector. If all the columns are already factors with all the levels set in the correct order, you could just use as.numeric on the factor values to convert them to integers

  • Related