user1100747
user1100747

Reputation: 29

R table conversion

Hello I am working with a table with these characteristics:

2000    0.051568
2000    0.04805
2002    0.029792
2002    0.056141
2008    0.047285
2008    0.038989

And I need to convert it to something like this:

2000       2002      2008

0.051568   0.029792  0.047285
0.04805    0.056141  0.038989

I would be grateful if somebody could give me a solution.

Upvotes: 0

Views: 148

Answers (3)

Josh O'Brien
Josh O'Brien

Reputation: 162321

Here's a relatively simple solution:

# CREATE ORIGINAL DATA.FRAME
df <- read.table(text="2000    0.051568
2000    0.04805
2002    0.029792
2002    0.056141
2008    0.047285 
2008    0.038989", header=FALSE)
names(df) <- c("year", "value")

# MODIFY ITS LAYOUT
df2 <- as.data.frame(split(df$value, df$year))
df2
#      X2000    X2002    X2008
# 1 0.051568 0.029792 0.047285
# 2 0.048050 0.056141 0.038989

Upvotes: 2

Brian Diggs
Brian Diggs

Reputation: 58825

I'm guessing you are new to R, so I'm going to guess what you mean and give you some more correct terminology. If I guess wrong, then at least this may help you to clarify the question.

In R, a table is a special case of a matrix that arises from cross-tabulation. What I think you have (or want) to start with is a data.frame. A data.frame is a set of columns with potentially different types, but all the same length; it is "rectangular" in that sense. Generally, elements in the same positions in the columns (that is, each row) of a data.frame are related to each other. The columns of a data.frame have names, as can the rows.

long <- data.frame(year=c(2000,2000,2002,2002,2008,2008),
                   val=c(0.051568, 0.04805, 0.029792, 
                         0.056141, 0.047285, 0.038989))

Which when printed looks like

> long
  year      val
1 2000 0.051568
2 2000 0.048050
3 2002 0.029792
4 2002 0.056141
5 2008 0.047285
6 2008 0.038989

By itself, this isn't enough, because for your desired output, you need to specify which value for, say, 2000 is in the first row and which is in the second (etc., if there were more). In your example, it is just the order they are in.

long$targetrow = 1:2

Which makes long now look like

> long
  year      val targetrow
1 2000 0.051568         1
2 2000 0.048050         2
3 2002 0.029792         1
4 2002 0.056141         2
5 2008 0.047285         1
6 2008 0.038989         2

Now you can use reshape on it.

reshape(long, idvar="targetrow", timevar="year",  direction="wide")

which gives

> reshape(long, idvar="targetrow", timevar="year",  direction="wide")
  targetrow val.2000 val.2002 val.2008
1         1 0.051568 0.029792 0.047285
2         2 0.048050 0.056141 0.038989

More complicated transformations are possible using the reshape2 package, but this should get you started.

Upvotes: 1

Seb
Seb

Reputation: 5497

probably i am understanding this wrong but is ?reshape what you are looking for? from the examples:

summary(Indometh) 
wide <- reshape(Indometh, v.names="conc", idvar="Subject", timevar="time", direction="wide")

wide

Upvotes: 0

Related Questions