[R] Multiply list objects

Uwe Ligges ligges at statistik.tu-dortmund.de
Fri Jun 17 17:18:32 CEST 2011


I still do not get the point for what task this expansion of data may be 
useful, by I guess you want (in this case probably very inefficient, but 
other can work out how to improve if interested) to insert after

k <- lapply(h, function (x) x*0)

the lines:

for(i in seq_along(k)){
     temp <- rep(k[i], ncol(k[[i]]))
     names(temp) <- colnames(k[[i]])
     k[i] <- list(temp)
     for(j in seq_along(colnames(h[[i]]))){
         k[[i]][[j]][j,] <- k[[i]][[j]][,j] <- h[[i]][,j]
     }
}


Uwe Ligges




On 17.06.2011 13:48, Mathijs de Vaan wrote:
> Sorry, forgot to quote:
>
> Hi,
>
> I am trying to use the objects from the list below to create more objects.
> For each year in h I am trying to create as many objects as there are B's
> keeping only the values of B. Example for 1999:
>
> $`1999`$`8025`
>        B
> B      8025 8026 8027 8028 8029
>    8025    1    1    1    0    0
>    8026    1    0    0    0    0
>    8027    1    0    0    0    0
>    8028    0    0    0    0    0
>    8029    0    0    0    0    0
>
> $`1999`$`8026`
>        B
> B      8025 8026 8027 8028 8029
>    8025    0    1    0    0    0
>    8026    1    1    1    0    1
>    8027    0    1    0    0    0
>    8028    0    0    0    0    0
>    8029    0    1    0    0    0
>
> $`1999`$`8027`
>        B
> B      8025 8026 8027 8028 8029
>    8025    0    0    1    0    0
>    8026    0    0    1    0    0
>    8027    1    1    1    0    1
>    8028    0    0    0    0    0
>    8029    0    0    1    0    0
>
> $`1999`$`8028`
>        B
> B      8025 8026 8027 8028 8029
>    8025    0    0    0    0    0
>    8026    0    0    0    0    0
>    8027    0    0    0    0    0
>    8028    0    0    0    1    1
>    8029    0    0    0    1    0
>
> $`1999`$`8029`
>        B
> B      8025 8026 8027 8028 8029
>    8025    0    0    0    0    0
>    8026    0    0    0    0    1
>    8027    0    0    0    0    1
>    8028    0    0    0    0    1
>    8029    0    1    1    1    1
>
> Any suggestions? You help is very much appreciated!
>
> DF = data.frame(read.table(textConnection("  A  B  C
> 80  8025  1995
> 80  8026  1995
> 80  8029  1995
> 81  8026  1996
> 82  8025  1997
> 82  8026  1997
> 83  8025  1997
> 83  8027  1997
> 90  8026  1998
> 90  8027  1998
> 90  8029  1998
> 84  8026  1999
> 84  8027  1999
> 85  8028  1999
> 85  8029  1999"),head=TRUE,stringsAsFactors=FALSE))
>
> e<- function(y) crossprod(table(DF[DF$C %in% y, 1:2]))
> years<- sort(unique(DF$C))
> f<- as.data.frame(embed(years, 3))
> g<-lapply(split(f, f[, 1]), e)
> h<-lapply(g, function (x) ifelse(x>0,1,0))
>
>
>
> 2011/6/17 Uwe Ligges<ligges at statistik.tu-dortmund.de>
>
>> Since this example is not reproducible (and you have not quuoted any former
>> code) I can only give advice "in principle":
>>
>> 1. Never use 1:length(x) since this will seriously fail if x is a length 0
>> object. Instead, use seq_along(x)
>> 2. If k is a list, then you probably want to use doubled brackets in
>> k[[year]] for a scalar valued "year".
>>
>> Uwe Ligges
>>
>>
>>
>>
>> On 16.06.2011 23:49, mdvaan wrote:
>>
>>> I am still thinking about this problem. The solution could look something
>>> like this (it's net yet working):
>>>
>>> k<-lapply(h, function (x) x*0) # I keep the same format as h, but set all
>>> values to 0
>>> years<-c(1997:1999) # I define the years
>>> for (t in 1:length(years))
>>>         {
>>>         year = as.character(years[t])
>>>         ids = rownames(h[year][[1]])
>>>         }
>>>         for (m in 1:length(relevant_firms))
>>>                 {
>>>                 k[year][[m]]<-lapply(k[year], function (col)
>>> k[year][[1]][,m] =
>>> h[year][[1]][,m]&   k[year][[1]][m,] = h[year][[1]][m,])
>>>                 } # I am creating new list objects that should look like
>>> this
>>> k$'1999'$'8029' and I replace the values in the 8029 column and row by the
>>> original ones in h
>>>
>>> Any takes on this problem? Thank you very much!
>>>
>>> Best
>>>
>>>
>>> --
>>> View this message in context: http://r.789695.n4.nabble.com/**
>>> Multiply-list-objects-**tp3595719p3603871.html<http://r.789695.n4.nabble.com/Multiply-list-objects-tp3595719p3603871.html>
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________**________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>>> PLEASE do read the posting guide http://www.R-project.org/**
>>> posting-guide.html<http://www.R-project.org/posting-guide.html>
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>



More information about the R-help mailing list