Dput:这就是我的数据框:
list(structure(list(date_start = c("2016-11-18", "2016-11-18",
"2016-11-18", "2016-11-18", "2016-11-18", "2016-11-18", "2016-11-18",
"2016-11-18", "2016-11-18", "2016-11-18", "2016-11-18", "2016-11-18",
"2016-11-18", "2016-11-18", "2016-11-18", "2016-11-18", "2016-11-18",
"2016-11-18", "2016-11-18", "2016-11-18", "2016-11-18", "2016-11-18",
"2016-11-18", "2016-11-18", "2016-11-18"), date_stop = c("2016-12-17",
"2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17",
"2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17",
"2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17",
"2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17",
"2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17"), impressions = c("623631",
"1209005", "508541", "763007", "946022", "2383568", "540094",
"1254584", "3191299", "4190801", "2359474", "2575801", "70282",
"183058", "730293", "1480338", "355839", "1669711", "1404607",
"1987509", "305249", "2170586", "5367446", "4348979", "409597"
), spend = c(34486.51, 88862.98, 32249.5, 76337.45, 50648.22,
196258.1, 35084.39, 113426.58, 117047.22, 271978.4, 495136.59,
1808433.13, 4998.89, 45060.97, 21313.06, 120083.89, 15842.37,
142180.86, 57878.86, 166189.26, 10462.05, 152238.98, 482840.37,
718257.29, 29674.91), account_id = c("880218078740984", "880218078740984",
"880218078740984", "880218078740984", "880218078740984", "880218078740984",
"880218078740984", "880218078740984", "880218078740984", "880218078740984",
"880218078740984", "880218078740984", "880218078740984", "880218078740984",
"880218078740984", "880218078740984", "880218078740984", "880218078740984",
"880218078740984", "880218078740984", "880218078740984", "880218078740984",
"880218078740984", "880218078740984", "880218078740984"), campaign_id = c("6057756544100",
"6057756544100", "6057559580700", "6057559580700", "6057364739900",
"6057364739900", "6057310879700", "6057310879700", "6057308793900",
"6057308793900", "6057235101900", "6057235101900", "6057211751300",
"6057211751300", "6057176340100", "6057176340100", "6057174959500",
"6057174959500", "6056810791300", "6056810791300", "6056809420900",
"6056809420900", "6056722317300", "6056722004900", "6056615572700"
), device_platform = c("desktop", "mobile", "desktop", "mobile",
"desktop", "mobile", "desktop", "mobile", "desktop", "mobile",
"desktop", "mobile", "desktop", "mobile", "desktop", "mobile",
"desktop", "mobile", "desktop", "mobile", "desktop", "mobile",
"mobile", "mobile", "desktop")), .Names = c("date_start", "date_stop",
"impressions", "spend", "account_id", "campaign_id", "device_platform"
), class = "data.frame", row.names = c(NA, 25L)), structure(list(
date_start = c("2016-11-18", "2016-11-18", "2016-11-18",
"2016-11-18", "2016-11-18", "2016-11-18", "2016-11-18", "2016-11-18",
"2016-11-18"), date_stop = c("2016-12-17", "2016-12-17",
"2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17",
"2016-12-17", "2016-12-17"), impressions = c("1395054", "46582",
"384396", "4913690", "6829346", "3099996", "10362788", "501953",
"2004573"), spend = c(121206.88, 2884.55, 25574.57, 258769.07,
613721.98, 95262.81, 512381.86, 24024.67, 98838.29), account_id = c("880218078740984",
"880218078740984", "880218078740984", "880218078740984",
"880218078740984", "880218078740984", "880218078740984",
"880218078740984", "880218078740984"), campaign_id = c("6056615572700",
"6056341249700", "6056341249700", "6056331814900", "6056331814900",
"6056207498100", "6056207498100", "6056205799700", "6056205799700"
), device_platform = c("mobile", "desktop", "mobile", "desktop",
"mobile", "desktop", "mobile", "desktop", "mobile")), .Names = c("date_start",
"date_stop", "impressions", "spend", "account_id", "campaign_id",
"device_platform"), class = "data.frame", row.names = c(NA, 9L
)))
我正在尝试使用write.table函数将其写入文件
write.table(get(fileName), paste0("/home/rstudio/", fileName) , sep="," , append=TRUE, na = "NA", row.names=FALSE, col.names = TRUE)
我在尝试将其写入文件时出现此错误:
data.frame出错(list(date_start = c(“2016-11-18”,“2016-11-18”,“2016-11-18”,: 参数意味着不同的行数:25,9
答案 0 :(得分:1)
这里的问题是你试图将两个数据帧的列表写成.csv文件,不数据帧。您有几种可能的方法来解决这个问题。
写两个单独的.csv文件,每个数据框一个 - 例如,如果我们调用你的问题中定义的列表the_list:
write.csv(the_list[[1]], "file1.csv") # write.table as in your question is also fine
write.csv(the_list[[2]], "file2.csv")
由于两个数据帧都具有相同的列名,因此您可以将它们存储在同一个文件中,既可以作为一个数据帧,也可以作为一个数据帧直接存储在另一个数据帧之下。
# Convert to one dataframe, then write a .csv file
the_list[[1]][nrow(the_list[[1]]) + 1:nrow(the_list[[2]]), ] <- the_list[[2]]
write.csv(the_list[[1]], "filename.csv")
# Append the second file below the first
# There will be column names separating the two tables
# unless you set col.names=F for the second write.table
# Appending column names may produce a warning, but that's ok if it's what you want
write.table(the_list[[1]], "filename.csv", sep=",", append=TRUE, row.names=FALSE)
write.table(the_list[[2]], "filename.csv", sep=",", append=TRUE, row.names=FALSE)
在将数据框写入一个文件之前,还有其他方法可以尝试加入数据框,例如在较短的数据框底部添加仅包含NA
的行,然后使用cbind
或等效文件。在某些情况下,merge
等等可能有意义。