问题描述
我有一个5520 x 5520的大数据帧。每120行之后,我需要添加另一行。到5520,这些新行的值包含在一行的数据帧中。
使用rbind我将这些行添加到不需要的表的末尾。并给我一个错误:
fabioN2 <-rbind(fabioN2,auf2[1,]) Error in match.names(clabs,names(xi)) : names do not match prevIoUs names
使用带有add_row的tibble我也得到一个错误:
> fabioN2 %>% add_row(fabioN2,],.after = 120) Error: New rows can't add columns. x Can't find columns
fabioN2 ,
X1 ,
X2 ,
X3 ,
X4 ,and 5516 more in
。data .
fabioN2是大型数据帧,而auf2包含要添加到fabioN2的值。 毫无疑问,代码是错误的,并且基于错误,我必须匹配两个数据框的列名,以防止考虑5520个不同的列名。
任何人都知道如何轻松地在所需位置添加这些数据框吗?
解决方法
我希望我对您的问题有正确的逻辑...我对30行的data.frame做到了,每10行增加一行(因为在拟合输出方面,120可重现一个示例)在答案中。)
library(dplyr)
r <- 3 # your number is 46 (5520/120)
l <- 10 # your number is 120
# your long data.frame where you want to fit in ever l rows
df1 <- data.frame(dx = c("a","a","c","e","e"))
# your data.frame of one row to fit in every l rows
df2 <- data.frame(dy = c("X"))
# set colnames to be identical
names(df2) <- colnames(df1)
# use row number as ID and set it of as needed with the help of integer division
dff1 <- df1 %>%
dplyr::mutate(ID = dplyr::row_number()) %>%
dplyr::mutate(ID = ID + (ID-1) %/% l)
# repeat your one row df according to the quantity needed and use the row number with set off calculation
dff2 <- df2 %>%
dplyr::slice(rep(row_number(),r)) %>%
dplyr::mutate(ID = dplyr::row_number()) %>%
dplyr::mutate(ID = (ID) * l + ID)
# union both data.frames (I am supposing column types are identical!)
dff1 %>%
dplyr::union(dff2) %>%
dplyr::arrange(ID)
dx ID
1 a 1
2 a 2
3 a 3
4 a 4
5 a 5
6 a 6
7 a 7
8 a 8
9 a 9
10 a 10
11 X 11
12 c 12
13 c 13
14 c 14
15 c 15
16 c 16
17 c 17
18 c 18
19 c 19
20 c 20
21 c 21
22 X 22
23 e 23
24 e 24
25 e 25
26 e 26
27 e 27
28 e 28
29 e 29
30 e 30
31 e 31
32 e 32
33 X 33