[R] Melt and Rbind/Rbindlist

Shouro Dasgupta shouro at gmail.com
Sat Jan 31 23:57:47 CET 2015


I have climate data for 20 years for US counties (FIPS) in csv format, each
file represents one year of data. I have extracted the data and reshaped
the yearly data files using melt();

for (i in filelist) {
>   tmp1 <- as.data.table(read.csv(i,header=T, sep=","))
>   tmp2 <- melt(tmp1, id="FIPS")
>   tmp2$year <- as.numeric(substr(tmp2$variable,2,5))
>   tmp2$month <- as.numeric(substr(tmp2$variable,7,8))
>   tmp2$day <- as.numeric(substr(tmp2$variable,10,11))
> }


Should I *rbind *in the loop here as I have the memory?
So, the file (i) tmp2 looks like this:

FIPS  temp year month  date
> 1001 276.7936 2045 1 1/1/2045
> 1003 276.7936 2045 1 1/1/2045
> 1005 279.6452 2045 1 1/1/2045
> 1007 276.7936 2045 1 1/1/2045
> 1009 272.3748 2045 1 1/1/2045
> 1011 279.6452 2045 1 1/1/2045


My goal is calculate the mean by FIPS code by month/week, however, when I
use the following code, I get a NULL value.

mean.temp<- for (i in filelist) {tmp2[, list(temp.mean=lapply(.SD, mean),
> by=c("FIPS","year","month"), .SDcols=c("temp")]}


This works fine for individual years but with *for (i in filelist)*. What
am I doing wrong? Can include a rbind/bindlist in the loop to make a big
data.frame? Any suggestions will be highly appreciated. Thank you.

Sincerely,

Shouro

	[[alternative HTML version deleted]]



More information about the R-help mailing list