[R] Fwd: help needed

Jim Lemon jim at bitwrit.com.au
Fri May 25 10:15:45 CEST 2012


On 05/25/2012 02:45 AM, QAMAR MUHAMMAD UZAIR wrote:
> ...
 > I want to reshape it in the following FORMAT
 >
 >     1967    1968    1969    1970    1971    1972    1973    1974
 > 1    0.87    0.87    0.87    0.87                0.71
 > 2    0.87    0.87    0.87    0.87                0.72
 >
 > OBVIOUSLY, I HAVE A LARGE AMOUNT OF DATA TO WORK WITH.i would also
 > like to take into account the effect of leap year. For example if
 > 1969 in a leap year then the column under it, has to have 1 extra
 > reading.

Hi Qamar,
You can do something like this, assuming that your data frame is named 
"qmu" and the two columns are named "V1" and "V2":

# extract years from the dates
qmu$year<-as.numeric(sapply(strsplit(as.character(qmu$V1),"[.]"),"[",3))
# get a vector of the unique years
uyears<-unique(qmu$year)
# make an empty list
newqmu<-list()
# populate the list year by year
for(i in 1:length(uyears)) newqmu[[i]]<-qmu$V2[qmu$year==uyears[i]]

This will give you a list with the same characteristics as you 
described. You can't have a data frame with different column lengths, so 
you would have to pad the shorter columns with NA values if you want a 
data frame.

Jim



More information about the R-help mailing list