1

Problem

I'm trying to append new columns of data to an existing dataframe. The dataframe constists of multiple rows and columns, the data I'm trying to append is just one row and it should be added to the first row of the existing dataframe.

data is a dataframe with 3000 rows and 20 columns and a is a dataframe with 1 row and 12 columns.

for (o in 1:length(a)) {
  data[[1,(paste0(names(a)[o]))]]=a[o]
}

Error

replacing element in non-existent column

Expected result

I want to obtain a dataframe with 3000 rows and 32 columns, where the last 12 columns are missing values for the other rows, which I intend to populate later one by one.

I have already tried...

append(), cbind(), merge() with no success, so I came up with this loop but had no success either. I can't figure out where I'm making a mistake.

Maciej Jureczko
  • 1,560
  • 6
  • 19
  • 23
jayvee
  • 143
  • 1
  • 5
  • Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Oct 15 '17 at 12:40

2 Answers2

0

The cleanest solution I found:

generating 2 data frames:

df1 <- data.frame(x=c(1,2,3,4,5),y=c("a","b","c","d","e"))
df2 <- data.frame(z="added1",w="added2")

We need to add the columns of d2 to d1 and vice-versa.

df1.names <- names(df1)
df1[,names(df2)] <- NA
df2[,df1.names] <- NA

Since df1 doesn't have the columns of names(df2), the first instruction creates the columns and assign all his values to NA. the same for df2.

Finally, we can bind the two data frames:

df1 <- rbind(df2,df1)

       z      w  x    y
1 added1 added2 NA <NA>
2   <NA>   <NA>  1    a
3   <NA>   <NA>  2    b
4   <NA>   <NA>  3    c
5   <NA>   <NA>  4    d
6   <NA>   <NA>  5    e

In your case:

data.names <- names(data)
data[,names(a)] <- NA
a[,data.names] <- NA

data <- rbind(data,a)
0

Perhaps the best way would be to create a new dataframe that have the same number of rows as data, with the first row being a and other rows being missing :

a_to_append <- rbind(a, matrix(NA, ncol = 12, nrow = 2999, dimnames = list(NULL, colnames(a))))

Then, simply combine both data frames :

data_all <- cbind(data, a_to_append)

> head(data_all)
  X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20   X1   X2   X3   X4   X5   X6   X7   X8   X9  X10  X11  X12
1  2  2  2  2  2  2  2  2  2   2   2   2   2   2   2   2   2   2   2   2    1    1    1    1    1    1    1    1    1    1    1    1
2  2  2  2  2  2  2  2  2  2   2   2   2   2   2   2   2   2   2   2   2 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
3  2  2  2  2  2  2  2  2  2   2   2   2   2   2   2   2   2   2   2   2 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
4  2  2  2  2  2  2  2  2  2   2   2   2   2   2   2   2   2   2   2   2 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
5  2  2  2  2  2  2  2  2  2   2   2   2   2   2   2   2   2   2   2   2 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
6  2  2  2  2  2  2  2  2  2   2   2   2   2   2   2   2   2   2   2   2 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>