0

someone collected data by listing complications in different columns by Grade i.e

     G1  G2    G3
1   nil nil fever
2   nil nil other
3   nil nil   nil
4 trans nil   nil

I want to get 1 vector of individual complications and 1 vector of grade. i.e.

Complications (fever,other,nil, trans) Grade ( 3,3,nil,1)

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57

3 Answers3

2

With the help of this SO answer for my second line there, we can use the following dplyr solution:

library(dplyr)
df1 %>%
    ## First change all "nil" to NA, which allows us to use coalesce()
    mutate_all(na_if, "nil") %>%
    ## Then we use SO answer referenced above to get "Grade"
    mutate(Grade = apply(., 1, function(x) which(!is.na(x))[1])) %>%
    ## Next we can coalesce() the G columns together
    ## to get one Complications vector
    mutate(Complications = coalesce(G1, G2, G3)) %>%
    ## Finally we return just the desired columns
    select(Complications, Grade)

  Complications Grade
1         fever     3
2         other     3
3          <NA>    NA
4         trans     1
duckmayr
  • 16,303
  • 3
  • 35
  • 53
2

In base R, we can use max.col

Grade <- max.col(df1 != "nil") * NA^!rowSums(df1 != "nil")
Complications <- df1[cbind(seq_len(nrow(df1)), Grade)]
setNames(replace(Grade, is.na(Grade), "nil"), 
         replace(Complications, is.na(Complications), "nil"))
#   fever other   nil trans 
#     "3"   "3" "nil"   "1" 

data

df1 <- structure(list(G1 = c("nil", "nil", "nil", "trans"), G2 = c("nil", 
"nil", "nil", "nil"), G3 = c("fever", "other", "nil", "nil")), 
class = "data.frame", row.names = c("1", 
"2", "3", "4"))
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Here is a simple base R approach.

data[data == "nil"] <- NA
Complications <- apply(data,1,max,na.rm = TRUE)
Complications
      1       2       3       4 
"fever" "other"      NA "trans" 

Grade <- sapply(seq_along(Complications),
                function(x){which(data[x,] == Complications[x])[1]})
Grade
[1]  3  3 NA  1
data <- structure(list(G1 = c("nil", "nil", "nil", "trans"), G2 = c("nil", 
    "nil", "nil", "nil"), G3 = c("fever", "other", "nil", "nil")), row.names = c("1", 
    "2", "3", "4"), class = "data.frame")
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57