Home > Software design >  For loop through columns of dataframe to create columns using ifelse statment : R version 4.1.2
For loop through columns of dataframe to create columns using ifelse statment : R version 4.1.2

Time:02-24

Here is my issue: My dataframe has multiple events (events1 to events4, below) where the end of follow-up is time to ANY event. I converted this dataframe from wide to long format using survSplit. survSplit seems to only handle one event type at a time so I have decided to do it manually using a nested ifelse statement which seems to work until I want do the same operation in a for loop that will go from event column to event column (my real dataset has 33 outcome types). The loop fails and gives the following error message:

Error: Assigned data value must be compatible with existing data. x Existing data has 12 rows. x Assigned data has 0 rows. i Only vectors of size 1 are recycled. Run rlang::last_error() to see where the error occurred. In addition: Warning message: Unknown or uninitialised column: event.

 install.packages("survival")
 install.packages("dplyr")
 library(survival)
 library(dplyr)
 cutpoints.l <- c(1.25)
 f12 <- data.frame(id = 1:6,
              next.ivl= c(22.348, 1.837, 2.051,1.782,1.692, 1.730),
              event1 = c(0,1,0,0,1,0),
              event2 = c(1,0,0,0,0,1),
              event3 = c(0,0,1,1,0,0),
              event4 = c(0,0,0,0,0,0),
              enter= rep(0,6),
              end=c(22.348, 1.837,2.051,1.782,1.629,1.730))

 f12.split <- survSplit(Surv(next.ivl,event1)~.,f12, 
                   cut = cutpoints.l,
                   event = "event1",
                   start = "enter", 
                   end = "next.ivl",
                   episode = "ivl")

 f12.split <- f12.split %>%
   group_by(id) %>%
   mutate(n = ifelse(row_number() == 1, 1, 0))%>%
   mutate(N = ifelse(row_number() == n(), 1, 0))

 events<-grep("event", colnames(f12.split), value = TRUE)

 for (event in events) {
   print(event)
   f12.split$event<-ifelse(f12.split$N==1 & f12.split$event==1, ifelse(f12.split$n==1 & 
   f12.split$event==1,0,1),0)
   }

CodePudding user response:

It sounds like you want to reshape just the event* columns without doing the rest. This can be done directly using tidyr::pivot_longer or reshape2::melt:

tidyr::pivot_longer(f12, starts_with("event"), names_to = "event")
# # A tibble: 24 x 6
#       id next.ivl enter   end event  value
#    <int>    <dbl> <dbl> <dbl> <chr>  <dbl>
#  1     1    22.3      0 22.3  event1     0
#  2     1    22.3      0 22.3  event2     1
#  3     1    22.3      0 22.3  event3     0
#  4     1    22.3      0 22.3  event4     0
#  5     2     1.84     0  1.84 event1     1
#  6     2     1.84     0  1.84 event2     0
#  7     2     1.84     0  1.84 event3     0
#  8     2     1.84     0  1.84 event4     0
#  9     3     2.05     0  2.05 event1     0
# 10     3     2.05     0  2.05 event2     0
# # ... with 14 more rows

reshape2::melt(f12, id.vars = c("id", "next.ivl", "enter", "end"), variable.name = "event")
#    id next.ivl enter    end  event value
# 1   1   22.348     0 22.348 event1     0
# 2   2    1.837     0  1.837 event1     1
# 3   3    2.051     0  2.051 event1     0
# 4   4    1.782     0  1.782 event1     0
# 5   5    1.692     0  1.629 event1     1
# 6   6    1.730     0  1.730 event1     0
# 7   1   22.348     0 22.348 event2     1
# 8   2    1.837     0  1.837 event2     0
# 9   3    2.051     0  2.051 event2     0
# 10  4    1.782     0  1.782 event2     0
# 11  5    1.692     0  1.629 event2     0
# 12  6    1.730     0  1.730 event2     1
# 13  1   22.348     0 22.348 event3     0
# 14  2    1.837     0  1.837 event3     0
# 15  3    2.051     0  2.051 event3     1
# 16  4    1.782     0  1.782 event3     1
# 17  5    1.692     0  1.629 event3     0
# 18  6    1.730     0  1.730 event3     0
# 19  1   22.348     0 22.348 event4     0
# 20  2    1.837     0  1.837 event4     0
# 21  3    2.051     0  2.051 event4     0
# 22  4    1.782     0  1.782 event4     0
# 23  5    1.692     0  1.629 event4     0
# 24  6    1.730     0  1.730 event4     0
  • Related