将向量作为列添加到 data.frame 中,其中 fill = NA

Add vector as a column to a data.frame with fill = NA

我有一个长度为 3 (my_vector) 的向量。

我想将此向量作为新列绑定到现有的 data.frame (my_df)。

然而,data.frame 有 4 行。因此,在第 4 行中,新列值(或 my_df[4,3])应为 NA.

我怎样才能做到这一点?

当我执行 my_df$new_column <- my_vector 时,我收到以下错误消息: 替换有3行,数据有4


这里是my_df(包括4行):

> dput(my_df)

structure(list(
      person = c("Oleg", "Yurii", "Igor", "Mikhail"),
      role = structure(c(1L, 2L, 2L, 3L), class = "factor", .Label = c("EDITOR-IN-CHIEF", "DEPUTY EDITORS-IN-CHIEF", "Coordinating Editor"))),
      class = "data.frame", row.names = c(NA,  -4L)
)

my_vector(长度为3):

> dput(my_vector)

c("Lomonosov University", "Russian Academy of Sciences", "Institute of Acoustics, Moscow, Russia")

我们创建一个NA列,然后根据向量的长度分配'my_vector'。这里 seq_along(my_vector) return 1:3,因此前 3 个元素被替换为 'my_vector' 值

my_df$new_column <- NA_character_
my_df$new_column[seq_along(my_vector)] <- my_vector

或者如果我们在末尾使用 length<-

填充 NA,这可以一步完成
my_df$new_column <-  `length<-`(my_vector, nrow(my_df))

-输出

my_df
#   person                    role                             new_column
#1    Oleg         EDITOR-IN-CHIEF                   Lomonosov University
#2   Yurii DEPUTY EDITORS-IN-CHIEF            Russian Academy of Sciences
#3    Igor DEPUTY EDITORS-IN-CHIEF Institute of Acoustics, Moscow, Russia
#4 Mikhail     Coordinating Editor                                   <NA>

一个data.table选项

setDT(my_df)[, new := NA][, new := replace(new, seq_along(my_vector), my_vector)]

给予

> my_df
    person                    role                                    new
1:    Oleg         EDITOR-IN-CHIEF                   Lomonosov University
2:   Yurii DEPUTY EDITORS-IN-CHIEF            Russian Academy of Sciences
3:    Igor DEPUTY EDITORS-IN-CHIEF Institute of Acoustics, Moscow, Russia
4: Mikhail     Coordinating Editor                                   <NA>

您可以对 my_vector 中的值进行子集化,其长度与 my_df 相同。

my_df$new_column <- my_vector[seq_len(nrow(my_df))]
my_df

#   person                    role                             new_column
#1    Oleg         EDITOR-IN-CHIEF                   Lomonosov University
#2   Yurii DEPUTY EDITORS-IN-CHIEF            Russian Academy of Sciences
#3    Igor DEPUTY EDITORS-IN-CHIEF Institute of Acoustics, Moscow, Russia
#4 Mikhail     Coordinating Editor                                   <NA>