Search code examples
rdataframevectordata-bindingfillna

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


I have a vector of length 3 (my_vector).

I want to bind this vector to an existing data.frame (my_df) as a new column.

However, the data.frame has 4 rows. Thus, in the 4th row, the new column value (or my_df[4,3]) should be NA.

How can I achieve this?

When I do my_df$new_column <- my_vector, I get the following error message: replacement has 3 rows, data has 4


Here is my_df (comprising 4 rows):

> 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)
)

And my_vector (of length 3):

> dput(my_vector)

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

Solution

  • We create a NA column and then assign the 'my_vector' based on the length of the vector. Here seq_along(my_vector) return 1:3, thus the first 3 elements are replaced with 'my_vector' values

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

    Or this can be done in a single step if we pad NA at the end by making use of length<-

    my_df$new_column <-  `length<-`(my_vector, nrow(my_df))
    

    -output

    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>