[R] data.frame tall skinny transformation

Michael Jones mdjones71 at gmail.com
Fri Oct 23 23:23:44 CEST 2009


Hi,

I have a data.frame that looks something like this.


feature   5637                1321N1
feature1 -0.568750616 -0.934748758
feature2 -0.913080902 -0.941455172
feature3 0.442477294 -0.257921866

I want to change it to look like this.

feature      cell.line     value
feature1    5637   -0.568750616
feature2    5637   -0.913080902
feature3    5637   0.442477294
feature1    1321N1  -0.934748758
feature2    1321N1 -0.941455172
feature3    1321N1  -0.257921866


I have tried to do it with for loops but it is very slow.

# Make Feature data tall skinny
tsFeatures = c()
tsCellLines = c()
tsValues = c()

for(aFeature in as.character(featureData$feature)){
      print(aFeature)
      for(cellLine in cellLines){
            tsCellLines = c(tsCellLines, as.character(cellLine))
            tsValues = c(tsValues, as.numeric(subset(featureData,
feature == aFeature, select = c(which(colnames(featureData) %in%
cellLine)))))
            tsFeatures = c(tsFeatures, aFeature)
      }
}
tsFeatureData = data.frame(features = tsFeatures, cell.line =
tsCellLines, value=tsValues)




More information about the R-help mailing list