[R] Attributing values to matrix according to names
Dear R-helpers Apologies for the basic question, but I just got stuck: I would like to write values from a vector into array cells with the same names count[1:10] 10010 10014 10015 10017 10030 10080 10100 10230 10250 10280 0 0 0 0 0 1 1 0 2 0 data[1:10,,1] [,1] [,2] [,3] [,4] [,5] 10010 NA NA NA NA NA 10014 NA NA NA NA NA 10015 NA NA NA NA NA 10016 NA NA NA NA NA 10017 NA NA NA NA NA 10100 NA NA NA NA NA 10140 NA NA NA NA NA 10150 NA NA NA NA NA 10160 NA NA NA NA NA 10170 NA NA NA NA NA length(count) [1] 2842 dim(data) [1] 4667 5 10 My operation should result in data[1:10,,1] [,1] [,2] [,3] [,4] [,5] 100100 NA NA NA NA 100140 NA NA NA NA 100150 NA NA NA NA 10016 NA NA NA NA NA 100170 NA NA NA NA 101001 NA NA NA NA 10140 NA NA NA NA NA 10150 NA NA NA NA NA 10160 NA NA NA NA NA 10170 NA NA NA NA NA data[10014,1,1]-count[10014] works but data[names(count),1,1]-count[names(count)] Fails with Error: indexing outside limits. Many thanks for any help Manuel __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Attributing values to matrix according to names
Schneider, Manuel [EMAIL PROTECTED] writes: data[10014,1,1]-count[10014] works but data[names(count),1,1]-count[names(count)] Fails with Error: indexing outside limits. Well, you don't have a row named names(count) now do you? Try dropping the quotes. -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Attributing values to matrix according to names
Schneider, Manuel wrote: Dear R-helpers Apologies for the basic question, but I just got stuck: I would like to write values from a vector into array cells with the same names count[1:10] 10010 10014 10015 10017 10030 10080 10100 10230 10250 10280 0 0 0 0 0 1 1 0 2 0 data[1:10,,1] [,1] [,2] [,3] [,4] [,5] 10010 NA NA NA NA NA 10014 NA NA NA NA NA 10015 NA NA NA NA NA 10016 NA NA NA NA NA 10017 NA NA NA NA NA 10100 NA NA NA NA NA 10140 NA NA NA NA NA 10150 NA NA NA NA NA 10160 NA NA NA NA NA 10170 NA NA NA NA NA length(count) [1] 2842 dim(data) [1] 4667 5 10 My operation should result in data[1:10,,1] [,1] [,2] [,3] [,4] [,5] 100100 NA NA NA NA 100140 NA NA NA NA 100150 NA NA NA NA 10016 NA NA NA NA NA 100170 NA NA NA NA 101001 NA NA NA NA 10140 NA NA NA NA NA 10150 NA NA NA NA NA 10160 NA NA NA NA NA 10170 NA NA NA NA NA data[10014,1,1]-count[10014] works but data[names(count),1,1]-count[names(count)] You mean data[names(count),1,1] - count[names(count)] without the quotes ... Uwe Ligges Fails with Error: indexing outside limits. Many thanks for any help Manuel __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Attributing values to matrix according to names
Dear Peter and Uwe Thanks for your suggestions. However, data[names(count),1,1] - count[names(count)] Still gives the indexing problem, guess because not all element of count can be found in data. Found a way round this by temp-rep(NA, times=as.numeric(names(count[length(count)]))) temp[as.numeric(names(count))]-count rwname-rownames(data) for (i in 1:dim(data)[1]) data[i,1,1]-temp[as.numeric(rwname[i])] What works for me but I am convinced there is a far more elegant way. Kind regards Manuel -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Peter Dalgaard Sent: Thursday, October 20, 2005 4:29 PM To: Schneider, Manuel Cc: r-help@stat.math.ethz.ch Subject: Re: [R] Attributing values to matrix according to names Schneider, Manuel [EMAIL PROTECTED] writes: data[10014,1,1]-count[10014] works but data[names(count),1,1]-count[names(count)] Fails with Error: indexing outside limits. Well, you don't have a row named names(count) now do you? Try dropping the quotes. -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Attributing values to matrix according to names
Schneider, Manuel [EMAIL PROTECTED] writes: Dear Peter and Uwe Thanks for your suggestions. However, data[names(count),1,1] - count[names(count)] Still gives the indexing problem, guess because not all element of count can be found in data. Perhaps something like nm - names(count) nm - intersect(nm,dimnames(data)[[1]]) # or rownames(data) data[nm,1,1] - count[nm] Found a way round this by temp-rep(NA, times=as.numeric(names(count[length(count)]))) temp[as.numeric(names(count))]-count rwname-rownames(data) for (i in 1:dim(data)[1]) data[i,1,1]-temp[as.numeric(rwname[i])] What works for me but I am convinced there is a far more elegant way. Kind regards Manuel -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Peter Dalgaard Sent: Thursday, October 20, 2005 4:29 PM To: Schneider, Manuel Cc: r-help@stat.math.ethz.ch Subject: Re: [R] Attributing values to matrix according to names Schneider, Manuel [EMAIL PROTECTED] writes: data[10014,1,1]-count[10014] works but data[names(count),1,1]-count[names(count)] Fails with Error: indexing outside limits. Well, you don't have a row named names(count) now do you? Try dropping the quotes. -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html