[R] Filling in empty arrays/lists from using "paste" function
David Winsemius
dwinsemius at comcast.net
Tue Aug 25 23:56:06 CEST 2009
On Aug 25, 2009, at 3:52 AM, Uwe Ligges wrote:
> I highly suggest to rethink your problem in a way that you store the
> things labelled, e.g., TA1 to TA5 (as well as all the others) as the
> elements of a list TA. This way you have just one object TA that can
> easily be accessed by index operations and the code looks much
> cleaner in the end.
>
> Best,
> Uwe Ligges
Partial solutions considered?
TA <- by(a$pro, a$id, table) # creates the desired format
But has 1's in the locations where the values of sal should be.
for (i in names(TA) ) { # these are just the id's from "a"
for (j in levels(a$pro) ) {
TA[[i]][[j]] <- a[a$id==i & a$pro == j, "sal"] }} #wrong
Doesn't work yet because I have not figured out the right combination
of indexing on the r.h.s of the assignment. The logical test creates a
vector that is not registered with the right values of sal somehow.
Also the none-existent values get numeric(0) values. I'm thinking
that there may be a way for setting up a list with id and pro as
indices and sal values as entries and then reading from that
temporary list.
ll <-ist()
> for (i in 1:nrow(a) ) { ll[[ as.character(a$id[i]) ]]
[[ as.character(a$pro[i]) ]] <- a$sal[i] }
> ll
$idA1
bb gg
0 1
$idA2
uu tt
0 2
$idA3
ee bb
4 4
$idA4
tt gg
3 0
$idA5
uu ee
0 1
But then running the nested for loop causes an error when I try to
access non-existent ll subscripts. So I need an appropriate list
traversal function that will "know" where it is on the list so it can
use the subscript for an assignment from ll to TA. As yet have not
cracked that nut.
>
>
> Steven Kang wrote:
>> 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.
>> snipped
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list