简体   繁体   中英

Convert a list of character vectors of various length to a one column data.frame in R

I have a list of 400,000 character vectors of various length and I want to convert this list to a one column data.frame with each row being a string of characters concatenated from the original character vectors.

Here is an example.

lst <- list()

lst[[1]] <- letters[1:7]
lst[[2]] <- letters[3:5]
lst[[3]] <- LETTERS[15:26]
lst[[4]] <- letters[4:12]

I can convert this list to a data.frame like this:

df <- as.data.frame.AsIs(lst, stringsAsFactors=FALSE); df

When converted it looks like this (very close to what I want):

                                 lst
1                a, b, c, d, e, f, g
2                            c, d, e
3 O, P, Q, R, S, T, U, V, W, X, Y, Z
4          d, e, f, g, h, i, j, k, l

On the outside it looks OK and when I look at the class for the df object it says it's a "data.frame". However, when I look at it's structure I see I'm still dealing with a list.

str(df)

output:

'data.frame':   4 obs. of  1 variable:
 $ lst:List of 4
  ..$ : chr  "a" "b" "c" "d" ...
  ..$ : chr  "c" "d" "e"
  ..$ : chr  "O" "P" "Q" "R" ...
  ..$ : chr  "d" "e" "f" "g" ... 

I know that a data.frame is sort of a list but the desirable output is :

> str(df)
'data.frame':   4 obs. of  1 variable:
 $ lst: chr  "a,b,c,d,e,f,g" "c,d,e" "O,P,Q,R,S,T,U,V,W,X,Y,Z" "d,e,f,g,h,i,j,k,l"

I've seen very similar questions on SO but none of them met my expectations. I have tried all of the following but nothing has worked. Any help would be greatly appreciated.

1. mt <- as.matrix(unlist(lst, recursive = FALSE))

2. mt <- unlist(lst, recursive = FALSE)

3. df <- as.data.frame.AsIs(lst, stringsAsFactors=FALSE); df
    df$nlst <- as.character(rep(NA, nrow(df)))
    for(inti in 1:length(df)){
      df$nlst[inti] <- (df$lst[[inti]])
    }

4. df$nlst <- apply(df, 1, unlist)

5. df$nlst <- do.call(rbind, df$lst)

6. df <- as.data.frame(as.matrix(lst))

7. df <- plyr::ldply(lst, rbind)

Again, none of the above met my needs. Please help!

You can paste the output within the list and then call the data.frame

d1 <- data.frame(Col1=sapply(lst, toString), stringsAsFactors=FALSE)
str(d1)
#'data.frame':   4 obs. of  1 variable:
# $ Col1: chr  "a, b, c, d, e, f, g" "c, d, e" "O, P, Q, R, S, T, U, V, W, X, Y, Z" "d, e, f, g, h, i, j, k, l"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM