Home > database >  create a new dataframe depending on another data frame in R
create a new dataframe depending on another data frame in R

Time:03-02

I have the following data frame:

ID  T1  T2
a   no  no
a   no  yes
a   no  yes
b   no  no
b   yes no

If there is a yes for the column for the same ID then I want to write "yes" in another column, if there is never a "yes" for the same ID then I just want a "no" in the new data frame. So I want to create the following data frame:

ID  T1  T2
a   no  yes
b   yes no

So far I have this:

  for ( i in 1:end){
    sameCol=TRUE
    for (j in 1:end2){
      subID = table[j,1]
      if (table[j,i]=="yes"){
        if (table[j,1]==table[j-1,1]){
          isSAME=TRUE
        }
        isyes=TRUE
      }
      if((isyes==TRUE) & (isSAME==TRUE){
           table2[j,i]="yes"
      } else {
           table2[j,i]="no"
      }
    }
  }

But my code unfortunately doesn't work, what am I doing wrong?

CodePudding user response:

In R, for loops usually have easier alternatives.

With the library dplyr, you can summarise the data by group using group_by and summarise. Here, you want to have "yes" in the column if there is at least one "yes" in the column, otherwise "no". Use across to perform this across multiple columns.

library(dplyr)
df %>% 
  group_by(ID) %>% 
  summarise(across(T1:T2, ~ ifelse(any(.x == "yes"), "yes", "no")))

# A tibble: 2 x 3
  ID    T1    T2   
  <chr> <chr> <chr>
1 a     no    yes  
2 b     yes   no   

CodePudding user response:

df %>%  group_by(ID) %>% summarize_all(max)

Output:

  ID    T1    T2   
  <chr> <chr> <chr>
1 a     no    yes  
2 b     yes   no   
  • Related