Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将数据帧写入CSV_R_Csv - Fatal编程技术网

R 将数据帧写入CSV

R 将数据帧写入CSV,r,csv,R,Csv,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-1

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)
我在尝试将其写入文件时遇到以下错误:

数据框错误(列表(开始日期=c(“2016-11-18”、“2016-11-18”、“2016-11-18”),:
参数表示不同的行数:25,9这里的问题是您试图将两个数据帧的列表写入.csv文件,而不是数据帧。您有几种可能的解决方法

  • 编写两个单独的.csv文件,每个数据帧一个-例如,如果我们将您问题中定义的列表称为_列表:

    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)
    
    #转换为一个数据帧,然后编写一个.csv文件
    
    _list[[1]][nrow(_list[[1]])+1:nrow(_list[[2]]),](1)如何调用write.csv?(2)使用dput发布您的参数,以便我们能够复制它。write.table(get(fileName),paste0(/home/rstudio/),sep=“,”,append=TRUE,na=“na”,row.names=FALSE,col.names=TRUE)(1)请将此添加到您的消息Downvoted的文本中,这个问题是不可理解的。@user31264:即使dput的小输出也会与LIIMIT这个词交叉。有其他选择吗?