简体   繁体   中英

How to combine multiple data frame columns in R

I have a.csv file with demographic data for my participants. The data are coded and downloaded from my study database (REDCap) in a way that each race has its own separate column. That is, each participant has a value in each of these columns (1 if endorsed, 0 if unendorsed).

It looks something like this:

SubjID  Sex  Age  White  AA  Asian  Other

 001    F    62   0      1   0      0
 002    M    66   1      0   0      0

I have to use a roundabout way to get my demographic summary stats. There's gotta be a simpler way to do this. My question is, how can I combine these columns into one column so that there is only one value for race for each participant? (ie recoding so 1 = white, 2 = AA, etc, and only the endorsed category is being pulled for each participant and added to this column?)

This is what I would like for it to look:

SubjID  Sex  Age  Race

001     F    62   2
002     M    66   1

This is more or less similar to our approach with similar data from REDCap. We use pivot_longer for dummy variables. The final Race variable could also be made a factor. Please let me know if this is what you had in mind.

Edit: Added names_ptypes to pivot_longer to indicate that Race variable is a factor (instead of mutate ).

library(tidyverse)

df <- data.frame(
  SubjID = c("001", "002"),
  Sex = c("F", "M"),
  Age = c(62, 66),
  White = c(0, 1),
  AA = c(1, 0),
  Asian = c(0, 0),
  Other = c(0, 0)
)

df %>%
  pivot_longer(cols = c("White", "AA", "Asian", "Other"), names_to = "Race", names_ptypes = list(Race = factor()), values_to = "Value") %>%
  filter(Value == 1) %>%
  select(-Value)

Result:

# A tibble: 2 x 4
  SubjID Sex     Age Race 
  <fct>  <fct> <dbl> <fct>
1 001    F        62 AA   
2 002    M        66 White

Here is another approach using reshape2

df[df == 0] <- NA
df <- reshape2::melt(df, measure.vars = c("White", "AA", "Asian", "Other"), variable.name = "Race", na.rm = TRUE)
df <- subset(df, select = -value)

#  SubjID Sex Age  Race
#    002   M  66 White
#    001   F  62    AA 

Here's a base approach:

race_cols <- 4:7

ind <- max.col(df[, race_cols])

df$Race_number <- ind
df$Race <- names(df[, race_cols])[ind]

df[, -race_cols]

  SubjID Sex Age Race_number  Race
1    001   F  62           2    AA
2    002   M  66           1 White

Data from @Ben

df <- data.frame(
  SubjID = c("001", "002"),
  Sex = c("F", "M"),
  Age = c(62, 66),
  White = c(0, 1),
  AA = c(1, 0),
  Asian = c(0, 0),
  Other = c(0, 0)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM