简体   繁体   English

将列表的 data.frame 保存为 R 中的 csv 文件

[英]Save a data.frame of a list as a csv file in R

I have an object named output that is of class data.frame containing a list.我有一个名为output的对象,它data.frame包含列表的data.frame类。 I was wondering if it might be possible to save output as a csv file from R?我想知道是否可以将output保存为 R 的csv文件?

Here is my code:这是我的代码:

d <- data.frame(list(A = c(kap = 2, sap = 3), B = c(kap = 1, sap = 4)))

d[] <- lapply(d, as.list)  ## This step is required to show `TRUE` and `FALSE` in `output`

output <- data.frame(t(d), dap = c(T, F))

write.csv(output, "output.csv") # Error: unimplemented type 'list' in 'EncodeElement'

If we can create the data.frame after transposing the 'd', then it should also work如果我们可以在转置 'd' 后创建data.frame ,那么它也应该可以工作

output <-  data.frame(t(d), dap = c(TRUE, FALSE))
row.names(output) <- NULL

The lapply in the second line of code is looping over column and then applying the as.list ie it creating a list of lists所述lapply在代码的第二行上循环柱,然后再进行as.list即其创建listlists

lapply(d, as.list)
#$A
#$A[[1]]
#[1] 2

#$A[[2]]
#[1] 3


#$B
#$B[[1]]
#[1] 1

#$B[[2]]
#[1] 4

When we assign it to 'd' with keeping the same structure ( [] ), it would remain as a data.frame with list columns当我们将它分配给 'd' 并保持相同的结构 ( [] ) 时,它将保留为带有list列的data.frame

d[] <- lapply(d, as.list) 
str(d)
#'data.frame':  2 obs. of  2 variables:
# $ A:List of 2  # each column is a `list`
#  ..$ : num 2
#  ..$ : num 3
# $ B:List of 2
#  ..$ : num 1
#  ..$ : num 4

Now, we are doing the rbind现在,我们正在做rbind

rbind(d, dap = c(T, F))
#      A     B
#kap    2     1
#sap    3     4
#dap TRUE FALSE

and its transpose,和它的转置,

output <-  t(rbind(d, dap = c(T, F)))
output
#  kap sap dap  
#A 2   3   TRUE 
#B 1   4   FALSE

which may look like a regular data.frame output, but it is not这可能看起来像一个常规的 data.frame 输出,但它不是

str(output)
#List of 6
# $ : num 2
# $ : num 1
# $ : num 3
# $ : num 4
# $ : logi TRUE
# $ : logi FALSE
# - attr(*, "dim")= int [1:2] 2 3
# - attr(*, "dimnames")=List of 2
#  ..$ : chr [1:2] "A" "B"
#  ..$ : chr [1:3] "kap" "sap" "dap"

which is again wrappedd with data.frame再次用data.frame

str(data.frame(output))
#'data.frame':  2 obs. of  3 variables:
# $ kap:List of 2
#  ..$ A: num 2
#  ..$ B: num 1
# $ sap:List of 2
#  ..$ A: num 3
#  ..$ B: num 4
# $ dap:List of 2
#  ..$ A: logi TRUE
#  ..$ B: logi FALSE

still a data.frame with list columns.仍然是一个带有list列的data.frame One option is to loop over the list , unlist the list columns and wrap with data.frame一种选择是遍历listunlist list列并用data.framedata.frame

data数据

d <- data.frame(list(A = c(kap = 2, sap = 3), B = c(kap = 1, sap = 4)))

This happens because some of the columns of the data-frame are not columns but rather they are of type list, you can solve this by unnesting the data;发生这种情况是因为数据框的某些列不是列而是列表类型,您可以通过取消嵌套数据来解决此问题;

Here are two ways that you can do;您可以通过以下两种方式进行操作;

# Creating data
d <- data.frame(list(A = c(kap = 2, sap = 3), B = c(kap = 1, sap = 4)))
d[] <- lapply(d, as.list)  ## This step is required to show `TRUE` and `FALSE` in `output`

# Method one using dplyr (Recommended)
output <- dplyr::tibble(t(rbind(d, dap = c(T, F))))

# Method two unnesting data using tidyr
output <- tidyr::unnest(data.frame(t(rbind(d, dap = c(T, F)))))

# Output the data
write.csv(output, "output.csv")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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