I have a data frame (imported from an Excel worksheet where I have written a lists of strings row by row) and want to convert the rows into a list of vectors where each vector contains the non-missing cell values for that row:
eg:
#Sample data frame
dfX <- data.frame(C0 = c(1,2,3),
C1 = c("Apple","Apple","Pear"),
C2 = c("Banana","Orange", "Lemon"),
C3 = c("Pear","Melon", ""))
Which would be used to generate the following list:
myList = list(c("Apple","Banana", "Pear"),
c("Apple","Orange", "Melon"),
c("Pear","Lemon"))
Note the third vector is truncated to two elements as the cell contains an empty string. Also note that the index (C0) is dropped.
I have seen some examples which convert the data frame to a matrix and use the split function to then paste the results into the global environment, e.g.
list2env(setNames(split(as.matrix(dfX),
row(dfX)), paste0("Row",1:3)),
envir=.GlobalEnv)
But I was wondering if there were (a) a newer tidyverse function for handling this and (b) a way to populate straight to a list (I later want to lapply a function against that list). Also want the missing values handling on the way into the list if possible!
data.frame
is effectively a list of vectors. So,class(dfX) <- "list"
– Khashaaas.list(dfX)
. Either way, it would be probably make your life easier to convert the empty strings intoNA_character_
first. If you're reading your data in from csv, note the handy argumentna.strings
to do this during the import. – DanYdata.frame
into alist
of column vectors. OP wants alist
of row vectors. – Maurits Eversgather(dfX, var, val, -C0) %>% spread(C0, val) %>% map(c)
` – Khashaa