Wow,
I am very impressed by the volume and the quality of the answers I
got. I sincerely thank Leonid Gibiansky, Nick Locantore, Joseph S. Verducci,
Richard Yang, David James, Bert Gunter, Chuck Taylor, Dr. M.Sawada, Gérald
Jean, Vadim Kutsyy, Hui Huang and Peter Alspach for their help. I really
appreciate.
In order of appearance, here are the answers I got and my comments on the
approaches I tried.
Leonid Gibiansky wrote -----------------------
This should do the job:
for(i in 1:length(subplot$tag))
{
dataa$tag[dataa$tree == subplot$tree[i]] <-subplot$tag[i]
}
Nick Locantore wrote -----------------------
I would convert these to data frames, and then this will work,
asssuming that dataa and subplot are sorted by tree:
> dataa <- data.frame(dataa)
> subplot <- data.frame(subplot)
> a <- (!is.na(match(dataa$tree,subplot$tree)))
> dataa$tag[a] <- subplot$tag
Joseph S. Verducci wrote -----------------------
dataa[match(subplot[,1],dataa[,1]),4] <- 1
This approch did not work for me but Gérald Jean (see below) came with a
comparable solution and it worked for him.
Richard Yang wrote -----------------------
try
dataa$tag <- ifelse(dataa$tree == 1 | dataa$tree == 4, 1, 0)
David James wrote -----------------------
You can use match() to find the rows in dataa corresponding to
the trees in subplot, i.g.,
r <- match(subplot[,1], dataa[,1], nomatch=0)
dataa[r, 4] <- subplot[,2]
(the namatch is to handle trees in subplot missing from dataa, but probably
this should generate an error?).
Bert Gunter wrote -----------------------
If the tree values are just row numbers, then you could use:
dataa[subplot[,'tree'],'tag']<-subplot[,'tag']
However, my guess is that the tree numbers are not necessarily the row
numbers. Assuming that the tree numbers are UNIQUE, you could do
dataa[is.element(dataa[,'tree'],subplot[,'tree']),'tag']<-subplot[,'tag']
is.element is a wrapper for the match() function. If you have replicate
values in the 'tree' column, you may need to use match() directly.
This approach worked just fine YC
Chuck Taylor wrote -----------------------
dataa[subplot[,"tree" ],"tag"] <- subplot[,"tag"]
For some reasons, this approach replace tag values based on line number of
dataa matrix YC
Dr. M.Sawada wrote -----------------------
Try:
dataa[subplot[,1],4]<-1
For some reasons, this approach should replace tag values based on line
number of dataa matrix YC
Gérald Jean wrote -----------------------
the following should do the trick, if I understood correctly what you are
attempting to do.
dataa[subplot[, 'tree'], 'tag'] <- subplot[, 'tag']
For some reasons, this approach replace tag values based on line number of
dataa matrix YC
After a virtual discussion, Gérald came with this solution
dataa[match(subplot[, 'tree'], dataa[, 'tree']), 'tag'] <- subplot[, 'tag']
Vadim Kutsyy wrote -----------------------
try:
dataa[is.element(dataa[,1],subplot[,1]),4]<-1
This approach should work fine also YC
Hui Huang wrote -----------------------
> data2_ dataa
> data2[subplot[,1],4] _ subplot[,2]
This approach worked for other people
Peter Alspach wrote -----------------------
os> dataa <- merge(dataa[,-4], subplot, all.x=T)
os> dataa[is.na(dataa[,4]),4] <- 0
Regards,
Yves Claveau
-------------------------------------------
Yves Claveau, M.Sc. F.
Ontario Forest Research Institute
1235 Queen St. E.
Sault Ste Marie, ON
P6A 2E5
Ph.: (705) 946-2981 ext. 252
Fax.: (705) 946-2030
Email: yves.claveau@mnr.gov.on.ca
|