如何在R中添加带有额外列的新行?

发布于 2025-01-14 03:31:39 字数 463 浏览 4 评论 0原文

我试图将 for 循环的结果作为新行添加到数据帧中,但是当新结果的列数多于原始数据帧时,它会出现错误,如何将具有额外列的新结果添加到数据帧中将额外的列名称添加到原始数据框中?

例如 原始数据帧:

-______A BC

  • x1 1 1 1
  • x2 2 2 2
  • x3 3 3 3

我想得到

-______A BCD

  • x1 1 1 1 NA
  • x2 2 2 2 NA
  • x3 3 3 3 NA
  • X4 4 4 4 4

我尝试了 rbind (错误在 rbind(deparse.level, ...) 中: 参数的列数不匹配) 和 rbind_fill (错误:rbind.fill 的所有输入都必须是 data.frames) 和bind_rows(参数2必须有名称)

I was trying to add results of a for loop into a dataframe as new rows, but it gets an error when there is a new result with more columns than the original dataframe, how could I add the new result with extra columns to the dataframe with adding the extra column names to the original dataframe?

e.g.
original dataframe:

-______A B C

  • x1 1 1 1
  • x2 2 2 2
  • x3 3 3 3

I want to get

-______A B C D

  • x1 1 1 1 NA
  • x2 2 2 2 NA
  • x3 3 3 3 NA
  • X4 4 4 4 4

I tried rbind (Error in rbind(deparse.level, ...) :
numbers of columns of arguments do not match)
and rbind_fill (Error: All inputs to rbind.fill must be data.frames)
and bind_rows (Argument 2 must have names)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽容 2025-01-21 03:31:39

base R中,可以通过使用NA创建一个新列“D”,然后用4分配新行来完成。

df1$D <- NA
df1['x4', ] <- 4

-output

> df1
   A B C  D
x1 1 1 1 NA
x2 2 2 2 NA
x3 3 3 3 NA
x4 4 4 4  4

或者在单行中

rbind(cbind(df1, D = NA), x4 = 4)
   A B C  D
x1 1 1 1 NA
x2 2 2 2 NA
x3 3 3 3 NA
x4 4 4 4  4

关于错误在bind_rows中,当for循环输出不是命名向量时会发生这种情况

library(dplyr)
> vec1 <- c(4, 4, 4, 4)
> bind_rows(df1, vec1)
Error: Argument 2 must have names.
Run `rlang::last_error()` to see where the error occurred.

如果它是命名向量,那么它应该工作

> vec1 <- c(A = 4, B = 4, C = 4, D = 4)
> bind_rows(df1, vec1)
     A B C  D
x1   1 1 1 NA
x2   2 2 2 NA
x3   3 3 3 NA
...4 4 4 4  4

数据

df1 <- structure(list(A = 1:3, B = 1:3, C = 1:3), 
class = "data.frame", row.names = c("x1", 
"x2", "x3"))

In base R, this can be done by creating a new column 'D' with NA and then assign new row with 4.

df1$D <- NA
df1['x4', ] <- 4

-output

> df1
   A B C  D
x1 1 1 1 NA
x2 2 2 2 NA
x3 3 3 3 NA
x4 4 4 4  4

Or in a single line

rbind(cbind(df1, D = NA), x4 = 4)
   A B C  D
x1 1 1 1 NA
x2 2 2 2 NA
x3 3 3 3 NA
x4 4 4 4  4

Regarding the error in bind_rows, it happens when the for loop output is not a named vector

library(dplyr)
> vec1 <- c(4, 4, 4, 4)
> bind_rows(df1, vec1)
Error: Argument 2 must have names.
Run `rlang::last_error()` to see where the error occurred.

If it is a named vector, then it should work

> vec1 <- c(A = 4, B = 4, C = 4, D = 4)
> bind_rows(df1, vec1)
     A B C  D
x1   1 1 1 NA
x2   2 2 2 NA
x3   3 3 3 NA
...4 4 4 4  4

data

df1 <- structure(list(A = 1:3, B = 1:3, C = 1:3), 
class = "data.frame", row.names = c("x1", 
"x2", "x3"))
笔落惊风雨 2025-01-21 03:31:39

如果您列出 for 循环的元素,您可能会遇到类似的情况。

(l <- list(x1, x2, x3, x4, x5))
# [[1]]
# [1] 1 1 1
# 
# [[2]]
# [1] 2 2 2 2
# 
# [[3]]
# [1] 3 3
# 
# [[4]]
# [1] 4
# 
# [[5]]
# NULL

可以使用 do.call(rbind, .) 方法来rbind编辑,您的问题是,如何rbind多个不同的元素在长度中。

有一个 `length<-` 函数,您可以使用它来调整向量的长度。要了解长度,可以使用另一个函数 lengths,它可以为您提供每个列表元素的长度,您对其中的 maximum 感兴趣。

我包括了元素长度为 NULL 的特殊情况(我们的 l 的第 5th 元素);由于 NULL 的长度无法更改,因此将这些元素替换为 NA。

因此,您总共可以这样做:

do.call(rbind, lapply(replace(l, lengths(l) == 0L, NA), `length<-`, max(lengths(l))))
#       [,1] [,2] [,3] [,4]
# [1,]    1    1    1   NA
# [2,]    2    2    2    2
# [3,]    3    3   NA   NA
# [4,]    4   NA   NA   NA
# [5,]   NA   NA   NA   NA

或者,因为您可能想要一个具有漂亮的行和列名称的数据框:

ml <- max(lengths(l))
do.call(rbind, lapply(replace(l, lengths(l) == 0L, NA), `length<-`, ml)) |>
  as.data.frame() |> `dimnames<-`(list(paste0('x', 1:length(l)), LETTERS[1:ml]))
#     A  B  C  D
# x1  1  1  1 NA
# x2  2  2  2  2
# x3  3  3 NA NA
# x4  4 NA NA NA
# x5 NA NA NA NA

注意:使用 R >= 4.1。


数据:

x1 <- rep(1, 3); x2 <- rep(2, 4); x3 <- rep(3, 2); x4 <- rep(4, 1); x5 <- NULL

You probably have something like this, if you list the elements of your for loop.

(l <- list(x1, x2, x3, x4, x5))
# [[1]]
# [1] 1 1 1
# 
# [[2]]
# [1] 2 2 2 2
# 
# [[3]]
# [1] 3 3
# 
# [[4]]
# [1] 4
# 
# [[5]]
# NULL

Multiple elements can be rbinded using a do.call(rbind, .) approach, your problem is, how to rbind multiple elements that differ in length.

There's a `length<-` function with which you may adjust the length of a vector. To know to which length, there's another function, lengths, that gives you the lengths of each list element, where you are interested in the maximum.

I include the special case when an element has length NULL (our 5th element of l); since length of NULL cannot be changed, replace those elements with NA.

So altogether you may do:

do.call(rbind, lapply(replace(l, lengths(l) == 0L, NA), `length<-`, max(lengths(l))))
#       [,1] [,2] [,3] [,4]
# [1,]    1    1    1   NA
# [2,]    2    2    2    2
# [3,]    3    3   NA   NA
# [4,]    4   NA   NA   NA
# [5,]   NA   NA   NA   NA

Or, since you probably want a data frame with pretty row and column names:

ml <- max(lengths(l))
do.call(rbind, lapply(replace(l, lengths(l) == 0L, NA), `length<-`, ml)) |>
  as.data.frame() |> `dimnames<-`(list(paste0('x', 1:length(l)), LETTERS[1:ml]))
#     A  B  C  D
# x1  1  1  1 NA
# x2  2  2  2  2
# x3  3  3 NA NA
# x4  4 NA NA NA
# x5 NA NA NA NA

Note: R >= 4.1 used.


Data:

x1 <- rep(1, 3); x2 <- rep(2, 4); x3 <- rep(3, 2); x4 <- rep(4, 1); x5 <- NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文