[R] coercing a list to a data frame, lists in foreloops
Gabor Grothendieck
ggrothendieck at myway.com
Tue Feb 1 04:44:04 CET 2005
Benjamin M. Osborne <Benjamin.Osborne <at> uvm.edu> writes:
:
: 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.
The zoo package has facilities for this:
R> library(zoo)
R> # create a 3x2 zoo matrix whose rows correspond to time points 1,3,4
R> z <- zoo(matrix(1:6, 3), c(1,3,4))
R> # merge z with 0 dimensional zoo series having time points 1,2,3,4
R> merge(z, zoo(,c(1,2,3,4)))
z.1 z.2
1 1 4
2 NA NA
3 2 5
4 3 6
More information about the R-help
mailing list