简体   繁体   English

如何在 R 中复制一列并将值粘贴到另一列下方?

[英]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_rowsdata.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

base R基地 R

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 dplyr

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 data.table

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.

相关问题 (R) 如何根据 R 中的另一列和 ID 从一列复制粘贴值 - (R) How to copy paste values from one column based on another column and ID in R 在 R 中,如何根据特定的行/列条件有选择地将一个单元格“复制并粘贴”到另一个单元格中? - In R, how do I selectively 'copy and paste' a cell into another cell based on specific row/column criteria? 将列值粘贴到另一列 - paste column values to another column 如何在 R 的另一列的基础上粘贴行? - How to paste rows on the basis of another column in R? 如何从一列中提取一部分数据并使用 R 将其粘贴到另一列中? - how do I extract a part of data from a column and and paste it n another column using R? 你如何分隔一个列,其中值用逗号分隔 r - How do you separate a column where values are separated by commas r 如何将列中的特定值(如7)更改为R中的NA? - How do you change specific values (like 7) in a column to NA in R? 根据 R 中另一列中下面的行选择最后一个值 - Select last values based on rows below in another column in R 如何选择R中的一列而不是NA的最大值? - How do you select a max of one column and not NA's in another column in R? 在R中,如何根据另一列的子集定义df中的新列? - In R, how do you define a new column in a df as a function of a subset of another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM