R中的rbind命令位于同一数据集中

R中的rbind命令位于同一数据集中,r,dataframe,R,Dataframe,如果我们有两个拆分数据集,那么rbind命令很容易使用。 比如说 d1=structure(list(a = c(1L, 4L), b = c(2L, 5L), c = c(3L, 6L)), class = "data.frame", row.names = c(NA, -2L)) d2=structure(list(a = c(7L, 10L), b = c(8L, 11L), c = c(9L, 12L)), class = "data.frame"

如果我们有两个拆分数据集,那么rbind命令很容易使用。 比如说

d1=structure(list(a = c(1L, 4L), b = c(2L, 5L), c = c(3L, 6L)), class = "data.frame", row.names = c(NA, 
-2L))
d2=structure(list(a = c(7L, 10L), b = c(8L, 11L), c = c(9L, 12L)), class = "data.frame", row.names = c(NA, 
-2L))

new=rbind(d1,d2)
test=structure(list(a = c(1L, 4L), b = c(2L, 5L), c = c(3L, 6L), X = c(NA, 
NA), X.1 = c(NA, NA), d = c(7L, 10L), e = c(8L, 11L), f = c(9L, 
12L)), class = "data.frame", row.names = c(NA, -2L))
但是,如果数据没有分离,但是它们位于一个数据集中,那么如何使用rbind函数呢 比如说

d1=structure(list(a = c(1L, 4L), b = c(2L, 5L), c = c(3L, 6L)), class = "data.frame", row.names = c(NA, 
-2L))
d2=structure(list(a = c(7L, 10L), b = c(8L, 11L), c = c(9L, 12L)), class = "data.frame", row.names = c(NA, 
-2L))

new=rbind(d1,d2)
test=structure(list(a = c(1L, 4L), b = c(2L, 5L), c = c(3L, 6L), X = c(NA, 
NA), X.1 = c(NA, NA), d = c(7L, 10L), e = c(8L, 11L), f = c(9L, 
12L)), class = "data.frame", row.names = c(NA, -2L))
输出

   a  b  c
1  1  2  3
2  4  5  6
3  7  8  9
4 10 11 12
请尝试下一个代码:

#Code
new <- rbind(test[,1:3],setNames(test[,6:8],names(test[,1:3])))

假设数据列总是由
NA
列分隔,那么也许我们可以尝试下面的基本R代码,其中
rle
+
是。应用NA
+
过滤器
+
rbind

lst <- with(
  rle(sapply(
    test,
    function(x) all(!is.na(x))
  )),
  Filter(
    function(v) all(!is.na(v)),
    split.default(
      test,
      cut(seq_along(test), c(0, cumsum(lengths)))
    )
  )
)

out <- do.call(
  rbind,
  c(
    make.row.names = FALSE,
    lapply(lst, setNames, nm = names(lst[[1]]))
  )
)

您可以编写一个小函数,在
mapply
中使用
seq.int
,这将为您完成这项工作

f <- function(data, start, ncol) {
  s <- mapply(seq.int, start, start + ncol - 1)
  as.data.frame(apply(s, 1, function(s) unlist(data[, s])))
}

f分离
测试
数据集的逻辑是什么?他们是如何分开的?为什么删除第4列和第5列?
res <- f(test, start=c(1, 6), ncol=3)
res
#   V1 V2 V3
# 1  1  2  3
# 2  4  5  6
# 3  7  8  9
# 4 10 11 12

## or, if names wanted
res <- setNames(f(test, start=c(1, 6), ncol=3), names(test)[1:3])
res
#    a  b  c
# 1  1  2  3
# 2  4  5  6
# 3  7  8  9
# 4 10 11 12
test <- structure(list(a = c(1L, 4L), b = c(2L, 5L), c = c(3L, 6L), X = c(NA, 
NA), X.1 = c(NA, NA), d = c(7L, 10L), e = c(8L, 11L), f = c(9L, 
12L)), class = "data.frame", row.names = c(NA, -2L))