[英]How Do You Copy A Column and Paste Values Below Another Column in R?
I am looking to copy all the values of one column and paste them beneath an existing column within R. The thing is that these columns are on different sheets and it can't include adding a column, rather adding to the bottom of the existing column.我想复制一列的所有值并将它们粘贴到 R 内的现有列下方。问题是这些列在不同的工作表上,它不能包括添加列,而是添加到现有列的底部. There are values below those shown below and there are numerous other columns on both Sheets mentioned.
在下面显示的值下方有一些值,并且在提到的两个工作表上还有许多其他列。 It would replicate this:
它会复制这个:
Current:当前的:
SheetA$NumberOfClients SheetB$ClientTotal
20 16
10 19
23 12
24 27
25 31
26 ...
19
17
...
Need to Get To:需要到达:
SheetA$NumberOfClients
20
10
23
24
25
26
19
17
16
19
12
27
31
...
I have attempted to use rbind as well as the following code, but have received an "Error in libxlsxwriter: 'Worksheet row or column index out of range.'" when attempting to write the data frame as an xlsx file (note it is NOT too large of a file):我尝试使用 rbind 以及以下代码,但在尝试将数据框写入 xlsx 文件时收到“libxlsxwriter 中的错误:'工作表行或列索引超出范围'”(注意它不是文件太大):
SheetA$NumberOfClients <- data.frame(SheetA$NumberOfClients, SheetB$ClientTotal)
To append these to the column NumberOfClients
in SheetA
you will need more rows than SheetA
currently has.对于 append 这些到
SheetA
中的NumberOfClients
列,您将需要比SheetA
当前拥有的行更多的行。 This structure is weird, but if for whatever reason you wanted to do this, you could do:这种结构很奇怪,但如果出于某种原因你想这样做,你可以这样做:
SheetA[(nrow(SheetA)+1):(nrow(SheetA)+nrow(SheetB)), "NumberOfClients"] <- SheetB$ClientTotal
Since there is no sample data, I cant test but in the example:由于没有样本数据,我无法测试,但在示例中:
a <- data.frame(col1 = 1:20,
col2 = LETTERS[1:20])
b <- data.frame(col1 = 1:10,
col2 = LETTERS[1:10])
a[(nrow(a)+1):(nrow(a)+nrow(b)), "col1"] <- b$col1
Since it appears that your frames have other columns not included here, I think your easiest option is to use dplyr::bind_rows
or data.table::rbindlist
, since they are much more user-friendly when it comes to additional columns and/or different order of columns.由于您的框架似乎有其他列未包含在此处,我认为您最简单的选择是使用
dplyr::bind_rows
或data.table::rbindlist
,因为它们在涉及其他列和/或不同顺序时更加用户友好列。
Reproducible data, with an extra column thrown in for good measure.可重现的数据,为了更好的衡量而加入了一个额外的列。
SheetA <- structure(list(NumberOfClients = c(20L, 10L, 23L, 24L, 25L, 26L, 19L, 17L), other = c(0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, -8L), class = "data.frame")
SheetB <- structure(list(ClientTotal = c(16L, 19L, 12L, 27L, 31L)), class = "data.frame", row.names = c(NA, -5L))
No row-combining function is going to be able to infer how you want to combine columns of different names, so you need to be explicit and change the names so they match:没有行组合 function 将能够推断出您希望如何组合不同名称的列,因此您需要明确并更改名称以便它们匹配:
names(SheetB) <- "NumberOfClients"
From here, using rbind
itself, given your comment, is going to fail:从这里开始,根据您的评论,使用
rbind
本身将会失败:
rbind(SheetA, SheetB)
# Error in rbind(deparse.level, ...) :
# numbers of columns of arguments do not match
SheetB2 <- cbind(SheetB, subset(SheetA, select = setdiff(colnames(SheetA), colnames(SheetB)))[1,,FALSE][NA,,FALSE])
# Warning in data.frame(..., check.names = FALSE) :
# row names were found from a short variable and have been discarded
rbind(SheetA, SheetB2)
# NumberOfClients other
# 1 20 0
# 2 10 0
# 3 23 0
# 4 24 0
# 5 25 0
# 6 26 0
# 7 19 0
# 8 17 0
# 9 16 NA
# 10 19 NA
# 11 12 NA
# 12 27 NA
# 13 31 NA
The subset(.., setdiff(..))
part is to extra columns in SheetA
that we are missing in SheetB
. subset(.., setdiff(..))
部分是我们在SheetA
中缺少的SheetB
中的额外列。 The [1,,FALSE]
gives us just 1 row of that. [1,,FALSE]
只给了我们其中的 1 行。 The [NA,,FALSE]
gives us the appropriate "NA" for each of those columns. [NA,,FALSE]
为这些列中的每一列提供了适当的“NA”。 In this case, that portion (before the call to cbind
) effectively returns data.frame(other=NA_real_)
.在这种情况下,该部分(在调用
cbind
之前)有效地返回data.frame(other=NA_real_)
。 The reason to go through this?通过这个到go的原因? R has at least six different types of
NA
, and this is just a clear/declarative way to make sure that we get the correct one. R 至少有六种不同类型的
NA
,这只是一种明确/声明的方式来确保我们得到正确的。 If we don't, then it's possible one of the new columns will be coerced to a class that is different from the original SheetA
.如果我们不这样做,那么其中一个新列可能会被强制转换为与原始 SheetA 不同的
SheetA
。 (This might be a bit paranoid, but it is borne from many sessions of troubleshooting.) (这可能有点偏执,但它来自许多故障排除会话。)
dplyr::bind_rows(SheetA, SheetB)
# NumberOfClients other
# 1 20 0
# 2 10 0
# 3 23 0
# 4 24 0
# 5 25 0
# 6 26 0
# 7 19 0
# 8 17 0
# 9 16 NA
# 10 19 NA
# 11 12 NA
# 12 27 NA
# 13 31 NA
data.table::rbindlist(list(SheetA, SheetB), fill = TRUE)
# NumberOfClients other
# <int> <num>
# 1: 20 0
# 2: 10 0
# 3: 23 0
# 4: 24 0
# 5: 25 0
# 6: 26 0
# 7: 19 0
# 8: 17 0
# 9: 16 NA
# 10: 19 NA
# 11: 12 NA
# 12: 27 NA
# 13: 31 NA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.