[R] Transpose of the rows

arun smartpink111 at yahoo.com
Wed Jun 26 14:13:31 CEST 2013


 dta[,3]<-as.numeric(as.character(gsub(",",".",dta[,3])))
library(reshape2)

  dcast(dta,id~name,value.var="value")
#            id   AreaCycl   AreaEmer   AreaMax  HeightMax nrfilledseed
#1 TN00016-003A -1.3998944 -3.2771509 -3281.077   8.434493        1.825
#2 TN00016-014A -0.5843794 -0.8428928 -7939.444 -13.594147       12.200
#3 TN00031-014A  0.8001962  0.1741609 -1200.824 -25.868973           NA

A.K.


----- Original Message -----
From: Nico Met <nicomet80 at gmail.com>
To: R help <r-help at r-project.org>
Cc: 
Sent: Wednesday, June 26, 2013 7:37 AM
Subject: [R] Transpose of the rows

Dear R users,

I am using a big data matrix and need to transpose rows (formatting of
input matrix). I would like write a general code for this example.

for example: my input file is "dta"

dput(dta)
structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L), .Label = c("TN00016-003A", "TN00016-014A",
"TN00031-014A"), class = "factor"), name = structure(c(1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L), .Label = c("AreaCycl",
"AreaEmer", "AreaMax", "HeightMax", "nrfilledseed"), class = "factor"),
    value = structure(c(3L, 7L, 8L, 14L, 12L, 1L, 2L, 9L, 5L,
    13L, 11L, 10L, 4L, 6L), .Label = c("-0,584379415", "-0,842892819",
    "-1,399894415", "-1200,824457", "-13,59414667", "-25,86897333",
    "-3,277150907", "-3281,076515", "-7939,444248", "0,174160882",
    "0,800196212", "1,825", "12,2", "8,434493333"), class = "factor")),
.Names = c("id",
"name", "value"), class = "data.frame", row.names = c(NA, -14L
))

And output should look like this:

dput(dta1)
structure(list(id = structure(1:3, .Label = c("TN00016-003A",
"TN00016-014A", "TN00031-014A"), class = "factor"), AreaCycl = structure(c(
2L,
1L, 3L), .Label = c("-0,584379415", "-1,399894415", "0,800196212"
), class = "factor"), AreaEmer = structure(c(2L, 1L, 3L), .Label = c(
"-0,842892819",
"-3,277150907", "0,174160882"), class = "factor"), AreaMax = structure(c(
2L,
3L, 1L), .Label = c("-1200,824457", "-3281,076515", "-7939,444248"
), class = "factor"), HeightMax = structure(c(3L, 1L, 2L), .Label = c(
"-13,59414667",
"-25,86897333", "8,434493333"), class = "factor"), nrfilledseed = structure(
c(1L,
2L, NA), .Label = c("1,825", "12,2"), class = "factor")), .Names = c("id",
"AreaCycl", "AreaEmer", "AreaMax", "HeightMax", "nrfilledseed"
), class = "data.frame", row.names = c(NA, -3L))

Thanks for your help.

Nico

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list