Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change data.frame column into rows in R

Tags:

A <- c(1,6) B <- c(2,7) C <- c(3,8) D <- c(4,9) E <- c(5,0) df <- data.frame(A,B,C,D,E) df   A B C D E 1 1 2 3 4 5 2 6 7 8 9 0 

I would like to have this:

df    1  2 A  1  6  B  2  7 C  3  8 D  4  9     E  5  0 
like image 203
Travis Avatar asked Sep 08 '11 01:09

Travis


People also ask

How do I switch columns to rows in R?

Rotating or transposing R objects frame so that the rows become the columns and the columns become the rows. That is, you transpose the rows and columns. You simply use the t() command. The result of the t() command is always a matrix object.

Can you transpose a data frame in R?

How do I transpose data in R? Transposing data, e.g. in a data frame or matrix, is easy to do with the t() function. For example, if you want to transpose a data frame you can type t(dataFrame) .

How do I switch rows in a Dataframe in R?

To change the row order in an R data frame, we can use single square brackets and provide the row order at first place.

How to convert columns of an R data frame into rows?

Thus, to convert columns of an R data frame into rows we can use transpose function t. For example, if we have a data frame df with five columns and five rows then we can convert the columns of the df into rows by using as.data.frame (t (df)).

How to replace a value in a data frame in R?

You can use the following syntax to replace a particular value in a data frame in R with a new value: df [df == 'Old Value'] <- 'New value' You can use the following syntax to replace one of several values in a data frame with a new value: df [df == 'Old Value 1' | df == 'Old Value 2'] <- 'New value'

How do I add row names to a column in R?

Now, we can use the rownames_to_column function to add the row names of our data as variable: Note that the rownames_to_column command adds the row_names column at the first index position of our data frame (in contrast to our R syntax of Example 1).

How many rows and columns are there in a data frame?

As you can see based on the output of the RStudio console, our example data frame object contains five rows and two columns. If we want to use the values stored in one of our variables as row names of our data frame (or a matrix), we can use the row.names function in R:


2 Answers

If your dataframe is truly in that format, then all of your vectors will be character vectors. Or, you basically have a character matrix and you could do this:

data.frame(t(df)) 

It would be better, though, to just define it the way you want it from the get-go

df <- data.frame(c('A','B','C','D','E'),                   c(1, 2, 3, 4, 5),                  c(6, 7, 8, 9, 0)) 

You could also do this

df <- data.frame(LETTERS[1:5], 1:5, c(6:9, 0)) 

If you wanted to give the columns names, you could do this

df <- data.frame(L = LETTERS[1:5], N1 = 1:5, N2 = c(6:9, 0)) 

Sometimes, if I use read.DIF of Excel data the data gets transposed. Is that how you got the original data in? If so, you can call

read.DIF(filename, transpose = T) 

to get the data in the correct orientation.

like image 125
adamleerich Avatar answered Oct 11 '22 13:10

adamleerich


I really recommend data.table approach without manual steps becauce they are error-prone

A <- c(1,6) B <- c(2,7) C <- c(3,8) D <- c(4,9) E <- c(5,0) df <- data.frame(A,B,C,D,E) df  library('data.table') dat.m <- melt(as.data.table(df, keep.rownames = "Vars"), id.vars = "Vars") # https://stackoverflow.com/a/44128640/54964  dat.m 

Output

  A B C D E 1 1 2 3 4 5 2 6 7 8 9 0     Vars variable value  1:    1        A     1  2:    2        A     6  3:    1        B     2  4:    2        B     7  5:    1        C     3  6:    2        C     8  7:    1        D     4  8:    2        D     9  9:    1        E     5 10:    2        E     0 

R: 3.4.0 (backports)
OS: Debian 8.7

like image 37
Léo Léopold Hertz 준영 Avatar answered Oct 11 '22 11:10

Léo Léopold Hertz 준영