I have a data set (csv); e.g., ID samp1 samp2 samp3 samp4 G1 23 32 12 87 G2 85 45 49 76 G3 12 46 39 28 G4 73 26 18 13 and read it: data1<-(read.csv("Datafolder/rawdata.csv",header=T)) It is fine with reading as above, but, if it is read as transposed values like: data1<-t(read.csv("Datafolder/rawdata.csv",header=T)) ID "G1" "G2" "G3" "G4" samp1 "23" "85" "12" "73" ... all values are shown in " ". How to get this transposed data as numerical values? Thank you in advance, Jin -- View this message in context: http://www.nabble.com/reading-transposed-data-in-read.csv-tp24979445p24979445.html Sent from the R help mailing list archive at Nabble.com.
sandsky wrote:> > I have a data set (csv); e.g., > > ID samp1 samp2 samp3 samp4 > G1 23 32 12 87 > G2 85 45 49 76 > G3 12 46 39 28 > G4 73 26 18 13 > > and read it: > > data1<-(read.csv("Datafolder/rawdata.csv",header=T)) > > It is fine with reading as above, but, if it is read as transposed values > like: > > data1<-t(read.csv("Datafolder/rawdata.csv",header=T)) > > ID "G1" "G2" "G3" "G4" > samp1 "23" "85" "12" "73" > ... > > all values are shown in " ". > > How to get this transposed data as numerical values? > >Because ID is not numeric, and in a data frame each column must have the same data type. The least common denominator is "character". Written explicitly, you have to transpose the numeric part, and paste in the names later. This could be shortened, but the example below gives the idea. Dieter d = data.frame(ID=c("G1","G2"), samp1=1:2,samp2=5:6) d t(d) dt = as.data.frame(t(d[-1])) names(dt)= d[,1] -- View this message in context: http://www.nabble.com/reading-transposed-data-in-read.csv-tp24979445p24982142.html Sent from the R help mailing list archive at Nabble.com.
On Sat, 15 Aug 2009 09:20:04 +0200, Dieter Menne <dieter.menne at menne-biomed.de> wrote:> > > > sandsky wrote: >> >> I have a data set (csv); e.g., >> >> ID samp1 samp2 samp3 samp4 >> G1 23 32 12 87 >> G2 85 45 49 76 >> G3 12 46 39 28 >> G4 73 26 18 13 >> >> and read it: >> >> data1<-(read.csv("Datafolder/rawdata.csv",header=T)) >> >> It is fine with reading as above, but, if it is read as transposed >> values >> like: >> >> data1<-t(read.csv("Datafolder/rawdata.csv",header=T)) >> >> ID "G1" "G2" "G3" "G4" >> samp1 "23" "85" "12" "73" >> ... >> >> all values are shown in " ". >> >> How to get this transposed data as numerical values? >> >> > > Because ID is not numeric, and in a data frame each column must have the > same data type. The least common denominator is "character". > > Written explicitly, you have to transpose the numeric part, and paste in > the > names later. This could be shortened, but the example below gives the > idea. > > Dieter > > d = data.frame(ID=c("G1","G2"), samp1=1:2,samp2=5:6) > d > t(d) > dt = as.data.frame(t(d[-1])) > names(dt)= d[,1] > > >As Dieter has already said elements in each of column of a data.frame must be of the same type. But this is probably what you really after:> tdf <- read.table(textConnection("ID samp1 samp2 samp3 samp4+ G1 23 32 12 87 + G2 85 45 49 76 + G3 12 46 39 28 + G4 73 26 18 13 + "), header=T)> library(reshape) > recast(tdf, ...~ID)Using ID as id variables variable G1 G2 G3 G4 1 samp1 23 85 12 73 2 samp2 32 45 46 26 3 samp3 12 49 39 18 4 samp4 87 76 28 13>see reshape and plyr packages for this kind of data manipulation. -Vitalie. --
> tdf <- read.table(textConnection("ID samp1 samp2 samp3 samp4+ G1 23 32 12 87 + G2 85 45 49 76 + G3 12 46 39 28 + G4 73 26 18 13 + "), header=T)> library(reshape) > recast(tdf, ...~ID)Using ID as id variables variable G1 G2 G3 G4 1 samp1 23 85 12 73 2 samp2 32 45 46 26 3 samp3 12 49 39 18 4 samp4 87 76 28 13>see reshape and plyr packages for this kind of data manipulation. These packages can be helpful if you know what you want, but to start off it is sometimes more important to do things explicitly. While I think that plyr is definitively a must, getting the syntax right in reshape often needs longer than writing the explicit code. Dieter -- View this message in context: http://www.nabble.com/reading-transposed-data-in-read.csv-tp24979445p24985038.html Sent from the R help mailing list archive at Nabble.com.