Dear R users,
I am trying to fill in arrays (5 different according to distinct "id")
from objects produced from arbitrary data set below.
a <-
data.frame(id=rep(c("idA1","idA2","idA3","idA4","idA5"),2),pro=c("bb","uu","ee","tt","uu","gg","tt","bb","gg","ee"),sal=rpois(10,2))
id pro sal
1 idA1 bb 2
2 idA2 uu 0
3 idA3 ee 3
4 idA4 tt 2
5 idA5 uu 4
6 idA1 gg 3
7 idA2 tt 0
8 idA3 bb 1
9 idA4 gg 0
10 idA5 ee 5
My desired outputs (5 arrays/lists classified according to distinct "id"
field) are as follow:
TA1
bb ee gg tt uu
2 0 3 0 0
TA2
bb ee gg tt uu
0 0 0 0 0
TA3
bb ee gg tt uu
1 3 0 0 0
...... similarly for TA4 & TA5.
The above results were produced using TA1 <-
c(bb=TA1.bb,ee=TA1.ee,gg=TA1.gg,tt=TA1.tt,uu=TA1.uu), TA2 <-
c(bb=TA2.bb,ee=TA2.ee,gg=TA2.gg,tt=TA2.tt,uu=TA2.uu*)* etc for TA3~TA5.
Although these generate the output I desire, I would like to use a single
statement for producing 5 different arrays (instead of 5 different
statements)
I have tried the following codes, however the last statement (paste("T",
substring(i,3,4), sep="") <- c(bb ......) gives error message reading "Error
in paste("T", substring(i, 3, 4), sep = "") <- c(bb = paste(paste("T",
: target of assignment expands to non-language object"
for (i in unique(a$id))
for (j in unique(a$pro))
assign(paste(paste("T", substring(i,3,4), sep=""), j, sep="."),
sum(subset(a, a$id == i & a$pro == j)$sal))
paste("T", substring(i,3,4), sep="") <- c(bb=paste(paste("T",
substring(i,3,4), sep=""),j, sep="."),ee=paste(paste("T", substring(i,3,4),
sep=""),j, sep="."),
gg=paste(paste("T", substring(i,3,4), sep=""),j,
sep="."),tt=paste(paste("T", substring(i,3,4), sep=""),j, sep="."),
uu=paste(paste("T",
substring(i,3,4), sep=""),j, sep="."))
Your solution to this problem would be highly appreciated.
Steve
[[alternative HTML version deleted]]
______________________________________________
R-help@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.