Search code examples
rdataframestata

Error: "Columns of type list not supported yet" in R when trying to write dta file using the haven package


Three of my columns are acting weird even though they are stated as numeric in my dataframe. I was also unable to change the names of these columns using colnames

My steps:

  1. install.packages("haven")

  2. library(haven)

  3. write_dta(data = df, "path/mydata.dta")

My dataframe df

structure(list(WeekDaysAbove28 = structure(list(`tapply(testdlong$value > 28, ceiling(seq(nrow(testdlong))/7), ` = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L)), row.names = c("1", "2", "3", "4", "5", "6", "7", 
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
"19", "20"), class = "data.frame"), WDA28 = structure(list(`tapply(testdlong$value > 28, ceiling(seq(nrow(testdlong))/7), ` = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L)), row.names = c("1", "2", "3", "4", "5", "6", "7", 
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
"19", "20"), class = "data.frame"), WDBM5 = structure(list(`tapply(testdlong$value < -5, ceiling(seq(nrow(testdlong))/7), ` = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L)), row.names = c("1", "2", "3", "4", "5", "6", "7", 
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
"19", "20"), class = "data.frame")), row.names = c(NA, 20L), class = "data.frame")

The error "Columns of type list not supported yet" suggest I need to alter the information given in these columns somehow but I cannot figure out exactly what is wrong. I have 294 columns and when these three were excluded the write_dta command worked just fine


Solution

  • You have nested dataframe. Most probably that is because you ran the previous step (with tapply) incorrectly. Try this :

    result <- data.frame(lapply(df, `[[`, 1))
    result
    
    #   WeekDaysAbove28 WDA28 WDBM5
    #1                0     0     0
    #2                0     0     0
    #3                0     0     0
    #4                0     0     0
    #5                0     0     0
    #6                0     0     0
    #7                0     0     0
    #8                0     0     0
    #9                0     0     0
    #10               0     0     0
    #11               0     0     0
    #12               0     0     0
    #13               0     0     0
    #14               0     0     0
    #15               0     0     0
    #16               0     0     0
    #17               0     0     0
    #18               0     0     0
    #19               0     0     0
    #20               0     0     0
    
    str(result)
    #'data.frame':  20 obs. of  3 variables:
    # $ WeekDaysAbove28: int  0 0 0 0 0 0 0 0 0 0 ...
    # $ WDA28          : int  0 0 0 0 0 0 0 0 0 0 ...
    # $ WDBM5          : int  0 0 0 0 0 0 0 0 0 0 ...