[R] column names to row names

jim holtman jholtman at gmail.com
Wed Sep 17 15:26:07 CEST 2014


Use the 'tidyr' package:  your 'month' does not match your desired output -

> x <- structure(c(1961, 1961, 1961, 1961, 1, 1, 1, 1, 1, 2, 3
+         , 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
+         , 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27
+         , 28, 29, 30, 31, 32, 33, 34, 35, 36)
+     , .Dim = c(4L, 12L)
+     , .Dimnames = list(NULL, c("year", "month", "day", "A", "B", "C"
+         , "D", "E", "F", "G", "H", "I"))
+     )
> xdf <- as.data.frame(x)
> xdf
  year month day A B  C  D  E  F  G  H  I
1 1961     1   1 1 5  9 13 17 21 25 29 33
2 1961     1   2 2 6 10 14 18 22 26 30 34
3 1961     1   3 3 7 11 15 19 23 27 31 35
4 1961     1   4 4 8 12 16 20 24 28 32 36
> require(tidyr)
> require(dplyr)
> xdf %>% gather(station, discharge, -year, -month, -day)
   year month day station discharge
1  1961     1   1       A         1
2  1961     1   2       A         2
3  1961     1   3       A         3
4  1961     1   4       A         4
5  1961     1   1       B         5
6  1961     1   2       B         6
7  1961     1   3       B         7
8  1961     1   4       B         8
9  1961     1   1       C         9
10 1961     1   2       C        10
11 1961     1   3       C        11
12 1961     1   4       C        12
13 1961     1   1       D        13
14 1961     1   2       D        14
15 1961     1   3       D        15
16 1961     1   4       D        16
17 1961     1   1       E        17
18 1961     1   2       E        18
19 1961     1   3       E        19
20 1961     1   4       E        20
21 1961     1   1       F        21
22 1961     1   2       F        22
23 1961     1   3       F        23
24 1961     1   4       F        24
25 1961     1   1       G        25
26 1961     1   2       G        26
27 1961     1   3       G        27
28 1961     1   4       G        28
29 1961     1   1       H        29
30 1961     1   2       H        30
31 1961     1   3       H        31
32 1961     1   4       H        32
33 1961     1   1       I        33
34 1961     1   2       I        34
35 1961     1   3       I        35
36 1961     1   4       I        36
>

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Wed, Sep 17, 2014 at 8:28 AM, eliza botto <eliza_botto at hotmail.com> wrote:
> Dear useRs,
> I have a data frame "y"  starting from 1961 to 2010 in the following manner (where A,B,C ......, I are station names and the values uder these are "discharge" values.)
>> dput(y)
> structure(c(1961, 1961, 1961, 1961, 1, 1, 1, 1, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36), .Dim = c(4L, 12L), .Dimnames = list(NULL, c("year", "month", "day", "A", "B", "C", "D", "E", "F", "G", "H", "I")))
>
> I want it to be in the following manner "E" where the stations names are in a seperate column and all discharge values are in one column.
>> dput(E)
>
> structure(list(year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "1961", class = "factor"),     month = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,     2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,     4), day = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,     1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L,     4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), discharge = structure(c(1L,     12L, 23L, 31L, 32L, 33L, 34L, 35L, 36L, 2L, 3L, 4L, 5L, 6L,     7L, 8L, 9L, 10L, 11L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,     20L, 21L, 22L, 24L, 25L, 26L, 27L, 28L, 29L, 30L), .Label = c("1",     "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",     "2", "20", "21", "22", "23", "24", "25", "26", "27", "28",     "29", "3", "30", "31", "32", "33", "34", "35", "36", "4",     "5", "6", "7", "8", "9"), class = "factor"), station = c("A",    !
>   "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C", "D",     "D", "D", "D", "E", "E", "E", "E", "F", "F", "F", "F", "G",     "G", "G", "G", "H", "H", "H", "H", "I", "I", "I", "I")), .Names = c("year", "month", "day", "discharge", "station"), row.names = c(NA, 36L), class = "data.frame")
>
> I hope I followed all the instructions given to be by some fellows.
> Thankyou very much in advance.
> Eliza
>
>         [[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