R 将列表列表写入单独的CSV文件
我有一个列表(414个元素),其中包含不同长度的其他列表(从0到9)。每个子列表都有不同数量的行和列 一些子列表的长度为1,如下所示:R 将列表列表写入单独的CSV文件,r,list,export,R,List,Export,我有一个列表(414个元素),其中包含不同长度的其他列表(从0到9)。每个子列表都有不同数量的行和列 一些子列表的长度为1,如下所示: tables_list[[1]] [,1] [,2] [1,] "ID Number" "ABCD" [2,] "Code" "1239463"
tables_list[[1]]
[,1] [,2]
[1,] "ID Number" "ABCD"
[2,] "Code" "1239463"
[3,] "Version" "1"
[4,] "Name" "ABC"
[5,] "Status" "Open"
[6,] "Currency" "USD"
[7,] "Average" "No"
[8,] "FX Rate" "2.47"
tables_list[[17]]
[[1]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] "" "" "USD" "Balance" "Movement in" "Aggregate" "Overall" "" "Overall"
[2,] "" "" "" "brought forward" "year" "annual" "aggregate" "" "funded account"
[3,] "" "" "" "from previous" "" "information" "adjustments" "" ""
[4,] "" "" "" "year end" "" "" "" "" ""
[5,] "" "" "" "1" "2" "3" "4" "" "5"
[6,] "12" "Value 1" "" "0" "3,275,020" "3,275,020" "" "0" "3,275,020"
[7,] "13" "Value 2" "" "0" "0" "0" "" "0" "0"
[8,] "14" "Value 3" "" "0" "8,267,862" "8,267,862" "" "0" "8,267,862"
[9,] "15" "Value 4" "" "0" "(590,073,321)" "(590,073,321)" "" "0" "(590,073,321)"
[10,] "16" "Value 5" "" "0" "0" "0" "" "0" "0"
[11,] "17" "Value 6" "" "0" "0" "0" "" "0" "0"
[12,] "18" "Value 7" "" "0" "0" "0" "" "0" "0"
[13,] "19" "Value 8" "" "0" "0" "0" "" "0" "0"
[14,] "20" "Value 9" "" "0" "(459,222,782)" "(459,222,782)" "" "0" "(459,222,782)"
[[2]]
[,1] [,2] [,3] [,4]
[1,] "Theme" "Year" "Comment" "Created"
[2,] "Line 17 Column 2" "N/A" "Amounts are calculated according to recent standards" "XXXXXXXXXXXX"
[3,] "" "" "paid by XXXXXXXXXXXXX" ""
其他子列表的长度为2或更高,如下所示:
tables_list[[1]]
[,1] [,2]
[1,] "ID Number" "ABCD"
[2,] "Code" "1239463"
[3,] "Version" "1"
[4,] "Name" "ABC"
[5,] "Status" "Open"
[6,] "Currency" "USD"
[7,] "Average" "No"
[8,] "FX Rate" "2.47"
tables_list[[17]]
[[1]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] "" "" "USD" "Balance" "Movement in" "Aggregate" "Overall" "" "Overall"
[2,] "" "" "" "brought forward" "year" "annual" "aggregate" "" "funded account"
[3,] "" "" "" "from previous" "" "information" "adjustments" "" ""
[4,] "" "" "" "year end" "" "" "" "" ""
[5,] "" "" "" "1" "2" "3" "4" "" "5"
[6,] "12" "Value 1" "" "0" "3,275,020" "3,275,020" "" "0" "3,275,020"
[7,] "13" "Value 2" "" "0" "0" "0" "" "0" "0"
[8,] "14" "Value 3" "" "0" "8,267,862" "8,267,862" "" "0" "8,267,862"
[9,] "15" "Value 4" "" "0" "(590,073,321)" "(590,073,321)" "" "0" "(590,073,321)"
[10,] "16" "Value 5" "" "0" "0" "0" "" "0" "0"
[11,] "17" "Value 6" "" "0" "0" "0" "" "0" "0"
[12,] "18" "Value 7" "" "0" "0" "0" "" "0" "0"
[13,] "19" "Value 8" "" "0" "0" "0" "" "0" "0"
[14,] "20" "Value 9" "" "0" "(459,222,782)" "(459,222,782)" "" "0" "(459,222,782)"
[[2]]
[,1] [,2] [,3] [,4]
[1,] "Theme" "Year" "Comment" "Created"
[2,] "Line 17 Column 2" "N/A" "Amounts are calculated according to recent standards" "XXXXXXXXXXXX"
[3,] "" "" "paid by XXXXXXXXXXXXX" ""
我试图将这些列表导出到一个单独的csv文件,但我想不出一种方法。有人对如何处理这个问题有什么想法吗?我尝试使用mapply
,但不断出现以下错误:
Error in file(file, ifelse(append, "a", "w")) :
invalid 'description' argument
In addition: Warning message:
In if (file == "") file <- stdout() else if (is.character(file)) { :
Error in file(file, ifelse(append, "a", "w")) :
invalid 'description' argument
文件中的错误(文件,ifelse(附加“a”、“w”):
无效的“description”参数
此外:警告信息:
在if(file==“”)file中,首先您可以使用它,然后您可以以常规方式循环它
flattenlist <- function(x){
morelists <- sapply(x, function(xprime) class(xprime)[1]=="list")
out <- c(x[!morelists], unlist(x[morelists], recursive=FALSE))
if (sum(morelists)) {
Recall(out)
} else {
return(out)
}
}
l <- list(a=list(1:2, b=2:4),
b=c("A", "B", "C"),
z=1,
m=matrix(4:1, 2),
d=data.frame(x=1:4, y=c(1, 3, 2, 4))
)
l.f <- flattenlist(l)
n <- paste0("robj_", names(l.f), ".csv")
sapply(1:length(l.f), function(x) write.csv(l.f[[x]], file=n[x]))
flattlist首先,您可以使用它,然后您可以按常规方式循环
flattenlist <- function(x){
morelists <- sapply(x, function(xprime) class(xprime)[1]=="list")
out <- c(x[!morelists], unlist(x[morelists], recursive=FALSE))
if (sum(morelists)) {
Recall(out)
} else {
return(out)
}
}
l <- list(a=list(1:2, b=2:4),
b=c("A", "B", "C"),
z=1,
m=matrix(4:1, 2),
d=data.frame(x=1:4, y=c(1, 3, 2, 4))
)
l.f <- flattenlist(l)
n <- paste0("robj_", names(l.f), ".csv")
sapply(1:length(l.f), function(x) write.csv(l.f[[x]], file=n[x]))
plantlist可能,你应该输入一个你尝试过的代码。可能,你应该输入一个你尝试过的代码。这节省了我几个小时这节省了我几个小时