在导出为.csv文件之前,连接两个数据帧 [英] Join two dataframes before exporting as .csv files

查看:190
本文介绍了在导出为.csv文件之前,连接两个数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一份大型问卷,并且为不同问题(例如df1和df2)产生摘要频率表。

  a< -c(1:5)
b< -c(4,3,2,1, 1)
百分比< -c(40,30,20,10,10)
df1< -data.frame(a,b,Percent)
c& ,5,2,1)
百分比< -c(10,10,50,20,10)
df2 <-data.frame(a,c,Percent)
rm ,b,c,Percent)



我通常使用以下命令将数据框导出为csv文件: / p>

  write.csv(df1,file =df2.csv)
然而,由于我的问卷有很多问题,因此数据帧,我想知道是否有一种方法在R中组合不同的数据帧(例如用一个分隔线) ,并将这些导出到csv(然后最终在Excel中打开它们)?当我打开Excel,我因此将只有一个文件与所有我的问题数据框,一个在另一个。



提前感谢。

$。这是一个很简单的文件。 b $ b

解决方案

如果你的最终目标是一个Excel电子表格,我会研究一些在R中可用的工具直接写一个xls文件。就个人而言,我使用 XLConnect 包,但也有 xlsx 和几个 write.xls



我碰巧喜欢 XLConnect ,因为它允许在像下面这样的情况下进行一些方便的矢量化:

  require(XLConnect)

#在单个列表中输出数据框
#我添加了两个副本
dfs< - list(df1,df2,df1,df2)

#创建xls文件和一张表
#注意XLConnect似乎没有波浪号展开!
wb< - loadWorkbook(/ Users / jorane / Desktop / so.xls,create = TRUE)
createSheet(wb,Survey)

#对于每个数据帧
#注意+1在每个
n< - length(dfs)
行< - cumsum(c(1,sapply(dfs [ (wb,dfs,Survey,startRow = rows,startCol = 1,header = FALSE(n-1)],nrow)+ 1))


#如果你不调用saveWorkbook,什么也不会发生
saveWorkbook(wb)


$ b b

我指定 header = FALSE ,因为否则将写入每个数据帧的列标题。但是在xls文件的最上面添加一行并不是额外的工作。


I am working on a large questionnaire - and I produce summary frequency tables for different questions (e.g. df1 and df2).

a<-c(1:5)
b<-c(4,3,2,1,1)
Percent<-c(40,30,20,10,10) 
df1<-data.frame(a,b,Percent)
c<-c(1,1,5,2,1)
Percent<-c(10,10,50,20,10)
df2<-data.frame(a,c,Percent)
rm(a,b,c,Percent)

I normally export the dataframes as csv files using the following command:

write.csv(df1 ,file="df2.csv")

However, as my questionnaire has many questions and therefore dataframes, I was wondering if there is a way in R to combine different dataframes (say with a line separating them), and export these to a csv (and then ultimately open them in Excel)? When I open Excel, I therefore will have just one file with all my question dataframes in, one below the other. This one csv file would be so much easier than having individual files which I have to open in turn to view the results.

Many thanks in advance.

解决方案

If your end goal is an Excel spreadsheet, I'd look into some of the tools available in R for directly writing an xls file. Personally, I use the XLConnect package, but there is also xlsx and also several write.xls functions floating around in various packages.

I happen to like XLConnect because it allows for some handy vectorization in situations just like this:

require(XLConnect)

#Put your data frames in a single list
# I added two more copies for illustration
dfs <- list(df1,df2,df1,df2)

#Create the xls file and a sheet
# Note that XLConnect doesn't seem to do tilde expansion!
wb <- loadWorkbook("/Users/jorane/Desktop/so.xls",create = TRUE)
createSheet(wb,"Survey")

#Starting row for each data frame
# Note the +1 to get a gap between each
n <- length(dfs)
rows <- cumsum(c(1,sapply(dfs[1:(n-1)],nrow) + 1))

#Write the file
writeWorksheet(wb,dfs,"Survey",startRow = rows,startCol = 1,header = FALSE)
#If you don't call saveWorkbook, nothing will happen
saveWorkbook(wb)

I specified header = FALSE since otherwise it will write the column header for each data frame. But adding a single row at the top in the xls file at the end isn't much additional work.

这篇关于在导出为.csv文件之前,连接两个数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆