R:在唯一的数据框中附加由一列组成的多个数据框,并将此数据框导出为excel表
我有数据框,行数可变,只有一列我想合并成一个df并导出到Excel dfs之间没有允许我执行经典合并的共享信息 由于我有这么多的数据帧,您知道有什么高效的代码可以使这个操作变得简单和快速吗?(见示例) 谢谢 组合柱 但是,您可能必须使用自定义cbind函数():R:在唯一的数据框中附加由一列组成的多个数据框,并将此数据框导出为excel表,r,merge,R,Merge,我有数据框,行数可变,只有一列我想合并成一个df并导出到Excel dfs之间没有允许我执行经典合并的共享信息 由于我有这么多的数据帧,您知道有什么高效的代码可以使这个操作变得简单和快速吗?(见示例) 谢谢 组合柱 但是,您可能必须使用自定义cbind函数(): 实例 在撰写本文时,您还没有生成一个示例,因此我将给出一个应该有效的示例: 下面是我正在使用的数据的一个dput(复制它并在R中运行它,以获得与运行示例相同的数据): 假设我想把id和Gender结合起来: # first I'm g
实例 在撰写本文时,您还没有生成一个示例,因此我将给出一个应该有效的示例: 下面是我正在使用的数据的一个
dput
(复制它并在R中运行它,以获得与运行示例相同的数据):
假设我想把id
和Gender
结合起来:
# first I'm going to separate the data
a<-as.data.frame(df[,1])
b<-as.data.frame(df[,2])
# > a
# df[, 1]
# 1 10
# 2 14
# 3 17
# 4 18
#> b
# df[, 2]
# 1 F
# 2 F
# 3 M
# 4 F
# combining the data into a new data frame
d <- cbind.data.frame(a,b)
# > d
# df[, 1] df[, 2]
# 1 10 F
# 2 14 F
# 3 17 M
# 4 18 F
# exporting to excel
install.packages("xlsx")
library(xlsx)
write.xlsx(d, "c:/myspreadsheet.xlsx")
#首先我要分离数据
a b
#df[,2]
#1楼
#二楼
#3米
#4楼
#将数据合并到新的数据帧中
d
#df[,1]df[,2]
#110楼
#2 14楼
#317米
#4 18楼
#导出到excel
安装程序包(“xlsx”)
图书馆(xlsx)
write.xlsx(d,“c:/myspreadsheet.xlsx”)
尽量少做点事,让问题更清楚。显示示例输入,并明确该输入所需的输出是什么。这是我运行此代码时得到的错误消息:new_df I将答案更新为不同长度的地址列。这是未经测试的。如果它给出了一个错误,请在发布另一条评论之前尝试谷歌。非常感谢,该功能运行得非常好。唯一的问题是我必须输入全局环境中包含的所有数据帧的名称,它们是95!我又尝试了:DF_list=ls()combine,我只是在谷歌上搜索了“列出所有数据帧R”,发现了这个:dfs
new_df <- cbind.data.frame(df1[,1],df2[,3])
library(plyr)
combined <- rbind.fill(df1[,1], df2[,3])
cbind.fill <- function(...){
nm <- list(...)
nm <- lapply(nm, as.matrix)
n <- max(sapply(nm, nrow))
do.call(cbind, lapply(nm, function (x)
rbind(x, matrix(, n-nrow(x), ncol(x)))))
}
library(xlsx)
write.xlsx(new_df, "c:/myspreadsheet.xlsx")
df <- structure(list(id = c(10, 14, 17, 18), Gender = structure(c(1L, 1L, 2L, 1L), .Label = c("F", "M"), class = "factor"), Col_Cold_1 = structure(c(4L, 2L, 1L, 3L), .Label = c("", "Bump", "muscle", "pain"), class = "factor"), Col_Cold_2 = structure(c(4L, 2L, 3L, 1L), .Label = c("", "NA", "pain", "sleep"), class = "factor"), Col_Cold_3 = structure(c(1L, 3L, 2L, 4L), .Label = c("NA", "hemaloma", "muscle", "pain" ), class = "factor"), Col_Hot_1 = structure(c(4L, 3L, 2L, 1L), .Label = c("", "Callus", "NA", "infection"), class = "factor"), Col_Hot_2 = structure(c(2L, 3L, 1L, 3L), .Label = c("infection", "medication", "twitching"), class = "factor"), Col_Hot_3 = structure(c(4L, 2L, 1L, 3L), .Label = c("", "flutter", "medication", "walking" ), class = "factor")), .Names = c("id", "Gender", "Col_Cold_1", "Col_Cold_2", "Col_Cold_3", "Col_Hot_1", "Col_Hot_2", "Col_Hot_3"), row.names = c(NA, 4L), class = "data.frame")
id Gender Col_Cold_1 Col_Cold_2 Col_Cold_3 Col_Hot_1 Col_Hot_2 Col_Hot_3
1 10 F pain sleep NA infection medication walking
2 14 F Bump NA muscle NA twitching flutter
3 17 M pain hemaloma Callus infection
4 18 F muscle pain twitching medication
# first I'm going to separate the data
a<-as.data.frame(df[,1])
b<-as.data.frame(df[,2])
# > a
# df[, 1]
# 1 10
# 2 14
# 3 17
# 4 18
#> b
# df[, 2]
# 1 F
# 2 F
# 3 M
# 4 F
# combining the data into a new data frame
d <- cbind.data.frame(a,b)
# > d
# df[, 1] df[, 2]
# 1 10 F
# 2 14 F
# 3 17 M
# 4 18 F
# exporting to excel
install.packages("xlsx")
library(xlsx)
write.xlsx(d, "c:/myspreadsheet.xlsx")