Replacing values of one matrix from another
Dear S-Plus users,
I am presently doing some data handling with S-Plus 6.0 under
Windows NT 4 and I am stuck with a problem. I want to replace values in a
matrix from values in a smaller one. I have:
dataa
tree dbh ht tag
1 12 7 0
2 15 9 0
3 18 12 0
4 10 5 0
5 21 14 0
and
subplot
tree tag
1 1
4 1
and I want to have
dataa
tree dbh ht tag
1 12 7 1
2 15 9 0
3 18 12 0
4 10 5 1
5 21 14 0
I thought using:
if (subplot[,1] = dataa[1,]) {dataa[,4] <- 1}
but S-Plus only uses the first line of each matrix. I then used a
less elegant solution by using the MERGE function with data frames:
datab <- merge(as.data.frame(dataa),as.data.frame(subplot),by="tree",
all.x=T)
datab$tag <- datab$tag.x+datab$tag.y
datac <- data.matrix(datab)
remove.col(datac, "tag.x",1)
remove.col(datac, "tag.y",1)
but for some strange reasons I cannot get rid of the columns created
by the merge function.
I tried an even less elegant approach by using FOR loops and IF but
I cannot get the results I want. What should I do?
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
|