I have a data frame with some missing values that I want to take from anther date frame and insert into the first. This works when the values I want to supplement miss in all rows of the first data frame.
Here is a working exaxmple:
dat <- data.frame(code = c("A11", "B22"),
age = c(NA, NA),
sex = c(NA, NA),
more = c(7, 4),
stringsAsFactors = FALSE)
age.and.sex <- read.table(textConnection("
code age sex
A11 15 m
B22 10 f
"), header = TRUE, stringsAsFactors = FALSE)
dat$sex[match(dat$code, age.and.sex$code)] <- age.and.sex$sex
dat$age[match(dat$code, age.and.sex$code)] <- age.and.sex$age
dat
code age sex more
1 A11 15 m 7
2 B22 10 f 4
The problem is that when I the values are not missing from all rows in the firest data frame, but I need to supplement only certain rows, I get an error.
Here is an example that does not work:
dat <- data.frame(code = c("A11", "B22", "C33"),
age = c(NA, NA, 12),
sex = c(NA, NA, "m"),
more = c(7, 4, 9),
stringsAsFactors = FALSE)
age.and.sex <- read.table(textConnection("
code age sex
A11 15 m
B22 10 f
"), header = TRUE, stringsAsFactors = FALSE)
dat$sex[match(dat$code, age.and.sex$code)] <- age.and.sex$sex
Error in dat$age[match(dat$code, age.and.sex$code)] <- age.and.sex$age :
NAs are not allowed in subscripted assignments
dat$age[match(dat$code, age.and.sex$code)] <- age.and.sex$age
Error in dat$age[match(dat$code, age.and.sex$code)] <- age.and.sex$age :
NAs are not allowed in subscripted assignments
I do not understand the error that R returns.
How do I need to change my code to get this to work again?
The result I'm trying to achieve is:
dat
code age sex more
1 A11 15 m 7
2 B22 10 f 4
3 C33 12 m 9
Thank you all for your help so far. But I must admit that I'm not quite happy with your solutions.
What you suggest is a four step approach: add one data frame to the other, move the values to their desired destination one by one, then delete the now superfluous secondary columns. As a schema, your solution looks something like this:
That seems awfully complicated to me!
When I look at my data, the solution that seems obvious to me has only one step: cut the "matrix" from one data frame and paste it into the "empty" area of the other data frame. Here is what it looks like in my mind:
And apparently that is actually possible:
dat[1:2,2:3] <- age.and.sex[1:2,2:3]
dat
code age sex more
1 A11 15 m 7
2 B22 10 f 4
3 C33 12 m 9
This, of course, only works if both data frames are ordered in the same way. Which is why I used match()
, which overcomes the problem when the rows aren't ordered – but fails when the number of rows is not the same.
Or is there a way to match()
, even if the number of rows is not the same?