Hi,
You could do this with ?merge() or ?join()
library(plyr)
new<-data.frame(ID=1:10,height1=NA)
combo<-data.frame(ID=c(3,7,9,10),height=c(185,NA,158,176))
?join(new,combo,type="left")[,-2]
#Joining by: ID
?#? ID height
#1?? 1???? NA
#2?? 2???? NA
#3?? 3??? 185
#4?? 4???? NA
#5?? 5???? NA
#6?? 6???? NA
#7?? 7???? NA
#8?? 8???? NA
#9?? 9??? 158
#10 10??? 176
A.K.
----- Original Message -----
From: lind35 <lindb at vcu.edu>
To: r-help at r-project.org
Cc:
Sent: Wednesday, November 28, 2012 9:42 PM
Subject: [R] Error 0ps.factor: level sets of factors are different
I have two data sets: "new" and "combo". Within both data
set, each row
pertains to a unique individual. The "combo" data set is a subset of
the
"new" data set, meaning that all individuals within "combo"
are within "new"
but not all individuals within "new" are within "combo".
Thus, nrow(new) >
nrow(combo).
Each data set has particular columns that represent the same sort of measure
(i.e., height, individual ID). However, the "new" data set does not
have the
height values. Moreover, some of the height values within "combo" are
blank.
I want R to identify a unique individual between data sets and then put the
individual's height from the "combo" data set into the
"new" data set. I
would like to do this in a for-loop, but I keep getting errors.
"ID" columns are new[,17] and combo[,11]
"height" columns are new[,14] and combo[,6]
for example I try:> for (i in 1:nrow(new)) {
+ temp <- combo[which(new[i,17] == combo[,11]),6]
+ if(length(temp) == 0) {new[i,14] <- NA
+ } else{new[i,14] <- combo[which(new[i,17] == combo[,11]),6]}
+ }
I can see how combo[,11] isn't the same as the new[i,17]. How do I tell R to
look at the ID value for the i'th row within "new", search the
11th column
of "combo" until it finds the matching ID, then place the height from
that
individual from "combo" into the appropriate cell of "new"?
And on top of
this, do this over and over until it's transferred all of the heights from
"combo" to the appropriate individuals in "new"?
Thanks for the help!
--
View this message in context:
http://r.789695.n4.nabble.com/Error-0ps-factor-level-sets-of-factors-are-different-tp4651246.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.