A. I have a large xts object which I converted to a data frame called allwins.sec so that I could change any non-NA object (in this case a number) in a column to the first part of the column name (the text up to the period(.)).
i.e.
Date, A.B, A.C, A.D, B.A, B.C, B.D, ...
1/1/11 NA NA 0.01 NA 0.05 NA
1/2/11 0.25 NA NA 0.45 NA 0.12
The output I'm looking for is:
Date, A.B, A.C, A.D, B.A, B.C, B.D, ...
1/1/11 NA NA A NA B NA
1/2/11 A NA NA B NA B
The code I tried to create the output was:
allwins.sec1 <- apply(allwins.sec, 2, function(i) ifelse(is.na(allwins.sec[,i]), NA, gsub("\\..*$", "" ,
colnames(allwins.sec[,i]))))
But I'm getting an error "Error in [.data.frame(allwins.sec, , i) :undefined columns selected"
Any thoughts on how to fix this?
B. My next step is also confusing me. My goal is to sum the text objects in each row (i.e. all the A's, B's, C, ...) and sort them from largest to smallest. Thanks in advance