Rbind 多个 data.frames 与行指示符
Rbind multiple data.frames with row indicators
当 rbind
处理多个数据帧时,我想指出前一个数据帧的起始位置。所以当使用:
df1<-data.frame(c(1,2,3,4),rnorm(1:4),rnorm(1:4),rnorm(1:4))
df2<-data.frame(c(1,2,3,4),rnorm(1:4),rnorm(1:4),rnorm(1:4))
dfTotal<-rbind(df1,df2)
我希望在 dfTotal 中有一个 df2 开始的指示器。
两个问题:
- 这可以做到吗?
- 是否有更好的方法让第一列从 1 变为 8?
我们可以使用 rbindlist
和 idcol
参数
library(data.table)
rbindlist(list(df1,df2), idcol='grp')
如果有多个数据集的模式 'df' 后跟数字,我们可以使用 mget
和 paste
来获取 `list
中的所有数据集
rbindlist(mget(paste0("df", 1:2)), idcol = "grp")
或使用 bind_rows
来自 dplyr
library(dplyr)
bind_rows(df1, df2, .id='grp')
或者我们可以使用base R
紧凑的方式
do.call(rbind, Map(cbind, ind = 1:2, mget(paste0("df", 1:2))))
这个使用 base
R 函数怎么样:
cbind(indicator=c(rep("df1", nrow(df1)), rep("df2", nrow(df2))) ,dfTotal<-rbind(df1,df2))
会给你:
indicator c.1..2..3..4. rnorm.1.4. rnorm.1.4..1 rnorm.1.4..2
1 df1 1 -0.50219235 0.1169713 -0.82525943
2 df1 2 0.13153117 0.3186301 -0.35986213
3 df1 3 -0.07891709 -0.5817907 0.08988614
4 df1 4 0.88678481 0.7145327 0.09627446
5 df2 1 -0.20163395 -0.3888542 -0.43808998
6 df2 2 0.73984050 0.5108563 0.76406062
7 df2 3 0.12337950 -0.9138142 0.26196129
8 df2 4 -0.02931671 2.3102968 0.77340460
数据
set.seed(100)
df1<-data.frame(c(1,2,3,4),rnorm(1:4),rnorm(1:4),rnorm(1:4))
df2<-data.frame(c(1,2,3,4),rnorm(1:4),rnorm(1:4),rnorm(1:4))
dfTotal<-rbind(df1,df2)
这是一个更长的基础 R 方法,它将 data.frames 放入列表中进行操作:
# put the data.frames into a list
dfList <- mget(ls(pattern="df[0-9]+"))
# append the list of data.frames into a single data.frame
dfTotal <- do.call(rbind, dfList)
# get the ID from the row names
dfTotal$id <- as.integer(gsub("^df(\d)+.*", "\1", rownames(dfTotal)))
要了解有关使用 data.frames 列表的更多信息,请查看 this post。
通过在 df1 和 df2 中添加 2 个变量来获取行指示符的简单方法,如下所示
df1<-data.frame(c(1,2,3,4),rnorm(1:4),rnorm(1:4),rnorm(1:4),map="d1")
df2<-data.frame(c(1,2,3,4),rnorm(1:4),rnorm(1:4),rnorm(1:4),map="d2")
dfTotal<-rbind(df1,df2)
c.1..2..3..4. rnorm.1.4. rnorm.1.4..1 rnorm.1.4..2 map
1 1 1.5211423 -0.05746568 0.7507580 d1
2 2 -0.5016556 0.33257341 -0.7042438 d1
3 3 -0.7154221 -0.79463908 -1.0391944 d1
4 4 -0.3255207 0.04130148 -1.4263133 d1
5 1 -1.5784721 0.58019130 -0.2091264 d2
6 2 -1.1682966 -0.17827840 1.3235675 d2
7 3 0.3025030 1.98774090 0.3537830 d2
8 4 2.5133713 -0.28664053 1.0521226 d2
当 rbind
处理多个数据帧时,我想指出前一个数据帧的起始位置。所以当使用:
df1<-data.frame(c(1,2,3,4),rnorm(1:4),rnorm(1:4),rnorm(1:4))
df2<-data.frame(c(1,2,3,4),rnorm(1:4),rnorm(1:4),rnorm(1:4))
dfTotal<-rbind(df1,df2)
我希望在 dfTotal 中有一个 df2 开始的指示器。
两个问题:
- 这可以做到吗?
- 是否有更好的方法让第一列从 1 变为 8?
我们可以使用 rbindlist
和 idcol
参数
library(data.table)
rbindlist(list(df1,df2), idcol='grp')
如果有多个数据集的模式 'df' 后跟数字,我们可以使用 mget
和 paste
来获取 `list
rbindlist(mget(paste0("df", 1:2)), idcol = "grp")
或使用 bind_rows
来自 dplyr
library(dplyr)
bind_rows(df1, df2, .id='grp')
或者我们可以使用base R
紧凑的方式
do.call(rbind, Map(cbind, ind = 1:2, mget(paste0("df", 1:2))))
这个使用 base
R 函数怎么样:
cbind(indicator=c(rep("df1", nrow(df1)), rep("df2", nrow(df2))) ,dfTotal<-rbind(df1,df2))
会给你:
indicator c.1..2..3..4. rnorm.1.4. rnorm.1.4..1 rnorm.1.4..2
1 df1 1 -0.50219235 0.1169713 -0.82525943
2 df1 2 0.13153117 0.3186301 -0.35986213
3 df1 3 -0.07891709 -0.5817907 0.08988614
4 df1 4 0.88678481 0.7145327 0.09627446
5 df2 1 -0.20163395 -0.3888542 -0.43808998
6 df2 2 0.73984050 0.5108563 0.76406062
7 df2 3 0.12337950 -0.9138142 0.26196129
8 df2 4 -0.02931671 2.3102968 0.77340460
数据
set.seed(100)
df1<-data.frame(c(1,2,3,4),rnorm(1:4),rnorm(1:4),rnorm(1:4))
df2<-data.frame(c(1,2,3,4),rnorm(1:4),rnorm(1:4),rnorm(1:4))
dfTotal<-rbind(df1,df2)
这是一个更长的基础 R 方法,它将 data.frames 放入列表中进行操作:
# put the data.frames into a list
dfList <- mget(ls(pattern="df[0-9]+"))
# append the list of data.frames into a single data.frame
dfTotal <- do.call(rbind, dfList)
# get the ID from the row names
dfTotal$id <- as.integer(gsub("^df(\d)+.*", "\1", rownames(dfTotal)))
要了解有关使用 data.frames 列表的更多信息,请查看 this post。
通过在 df1 和 df2 中添加 2 个变量来获取行指示符的简单方法,如下所示
df1<-data.frame(c(1,2,3,4),rnorm(1:4),rnorm(1:4),rnorm(1:4),map="d1")
df2<-data.frame(c(1,2,3,4),rnorm(1:4),rnorm(1:4),rnorm(1:4),map="d2")
dfTotal<-rbind(df1,df2)
c.1..2..3..4. rnorm.1.4. rnorm.1.4..1 rnorm.1.4..2 map
1 1 1.5211423 -0.05746568 0.7507580 d1
2 2 -0.5016556 0.33257341 -0.7042438 d1
3 3 -0.7154221 -0.79463908 -1.0391944 d1
4 4 -0.3255207 0.04130148 -1.4263133 d1
5 1 -1.5784721 0.58019130 -0.2091264 d2
6 2 -1.1682966 -0.17827840 1.3235675 d2
7 3 0.3025030 1.98774090 0.3537830 d2
8 4 2.5133713 -0.28664053 1.0521226 d2