[R] coercing a list to a data frame, lists in foreloops

Spencer Graves spencer.graves at pdf.com
Tue Feb 1 00:09:15 CET 2005


      A data.frame is a list, which may be why "mode(mansNew)" was "list". 

      Have you tried something like the following: 

 > tstList <- list(a=1:3, b=c(NA, 4, 6))
 > DF <- as.data.frame(tstList)
 > mode(DF)
[1] "list"
 > class(DF)
[1] "data.frame"
 > DF
  a  b
1 1 NA
2 2  4
3 3  6
 > DF2 <- DF[c(1,1:3),]
 > DF2[2,] <- 8:9
 > DF2
    a  b
1   1 NA
1.1 8  9
2   2  4
3   3  6

      This may not work with time series, but it clearly worked in this 
simple example. 

      hope this helps.  spencer graves

Benjamin M. Osborne wrote:

>I have a set of time-series climate data with missing entries.  I need to add
>rows for these missing entries to this data set.  The only way I know to do
>this is unsing a foreloop, but this won't work on a list.  I've tried to
>convert the list to a data frame, but that won't happen, either.
>
>I want to fill rows in this table:
>
>  
>
>>newtest[10:15,]
>>    
>>
>    yrmos yearmo snow.sum snow.mean snow.dep.mean prcp.sum prcp.mean tmin.min
>10 195410     NA       NA        NA            NA       NA        NA       NA
>11 195411     NA       NA        NA            NA       NA        NA       NA
>12 195412     NA       NA        NA            NA       NA        NA       NA
>13 195501     NA       NA        NA            NA       NA        NA       NA
>14 195502     NA       NA        NA            NA       NA        NA       NA
>15 195503     NA       NA        NA            NA       NA        NA       NA
>   tmin.mean tmax.max tmax.mean tmean.mean
>10        NA       NA        NA         NA
>11        NA       NA        NA         NA
>12        NA       NA        NA         NA
>13        NA       NA        NA         NA
>14        NA       NA        NA         NA
>15        NA       NA        NA         NA
>  
>
>
>from this one:
>
>  
>
>>mansNew[10:15,]
>>    
>>
>   yearmo snow.sum snow.mean snow.dep.mean prcp.sum prcp.mean  tmin.min
>10 195508    0.000 0.0000000       0.00000  29.5910 0.9545484        NA
>11 195509    0.000 0.0000000       0.00000   9.1948 0.3064933        NA
>12 195510   20.320 0.6554839            NA  13.8684 0.4473677        NA
>13 195511       NA        NA            NA       NA        NA -18.88889
>14 195512   52.324 1.6878710      53.01226   6.4770 0.2089355        NA
>15 195601   46.736 1.5076129            NA   8.0264 0.2589161        NA
>   tmin.mean   tmax.max  tmax.mean tmean.mean
>10        NA         NA         NA         NA
>11        NA         NA         NA         NA
>12        NA         NA         NA         NA
>13  -8.62963 12.2222222 -0.6481481  -4.638889
>14        NA -0.5555556 -9.3906810         NA
>15        NA         NA         NA         NA
>  
>
>This may be a problem:
>  
>
>>newtest<-as.data.frame(newtest)
>>mode(newtest)  ## returns "list"
>>    
>>
>[1] "list"
>  
>
>>mansNew<-as.data.frame(mansNew)
>>mode(mansNew)  ## returns "list"
>>    
>>
>[1] "list"
>  
>
>I've checked to make sure each column is a vector, but the coercion still is not
>allowed.
>
>This is the code with which I'm attempting to perform this manipulation, as well
>as the result:
>
>  
>
>>for (i in 1:100){
>>    
>>
>+ newtest[i,2:12]<-ifelse(is.element(newtest$yrmos[i],mansNew$yearmo),
>subset(mansNew, yearmo == newtest$yrmos[i])[,1:11], c(rep(NA,11)))
>+ }
>  
>
>>newtest[10:15,]
>>    
>>
>    yrmos yearmo snow.sum snow.mean snow.dep.mean prcp.sum prcp.mean tmin.min
>10 195410     NA       NA        NA            NA       NA        NA       NA
>11 195411 195411   195411    195411        195411   195411    195411   195411
>12 195412 195412   195412    195412        195412   195412    195412   195412
>13 195501 195501   195501    195501        195501   195501    195501   195501
>14 195502 195502   195502    195502        195502   195502    195502   195502
>15 195503 195503   195503    195503        195503   195503    195503   195503
>   tmin.mean tmax.max tmax.mean tmean.mean
>10        NA       NA        NA         NA
>11    195411   195411    195411     195411
>12    195412   195412    195412     195412
>13    195501   195501    195501     195501
>14    195502   195502    195502     195502
>15    195503   195503    195503     195503
>  
>
>
>subset...  should return only one row.  This may be a simple comma problem, but
>I think it has something to do with the lists.  Also, if there is a way to do
>this without the foreloop, I'd be happy to hear about it.
>Any suggestions will be appreciated.
>
>Thanks,
>Ben Osborne
>
>  
>




More information about the R-help mailing list