How to transpose rows into columns (and vice versa) and save to a new database?

Asked

Viewed 761 times

3

My database has 10 cases and 6 variables. I want to transpose the cases in column and save this transposition in a database.

1 answer

5


Use the function t:

dados <- head(cars)
dados
  speed dist
1     4    2
2     4   10
3     7    4
4     7   22
5     8   16
6     9   10
t(dados)
      1  2 3  4  5  6
speed 4  4 7  7  8  9
dist  2 10 4 22 16 10

The R has several ways to save data in files. My favorite is using the function write.csv:

write.csv(t(dados), file="dados.csv", row.names=FALSE)

In this way, the archive dados.csv will be created and will be imported into any other program that reads comma separated data, such as Excel, for example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.