user1440928
user1440928

Reputation: 1

How do I convert table formats in R

Specifically,

I used the following set up:

newdata <- tapply(mydata(#), list(mydata(X), mydata(Y)), sum)

I currently have a table that currently is listed as follows:

X= State, Y= County within State, #= a numerical total of something

  • __ Y1 Y2 Y3 Yn
  • X1 ## ## ## ##
  • X2 ## ## ## ##
  • X3 ## ## ## ##
  • Xn ## ## ## ##

What I need is a table listed as follows:

  • X1 Y1 ##
  • X1 Y2 ##
  • X1 Y3 ##
  • X1 Yn ##
  • X2 Y1 ##
  • X2 Y2 ##
  • X2 Y3 ##
  • X2 Yn ##
  • Xn Y1 ##
  • Xn Y2 ##
  • Xn Y3 ##
  • Xn Yn ##

Upvotes: 0

Views: 91

Answers (2)

Brian Diggs
Brian Diggs

Reputation: 58825

Some example data

mydata <- data.frame(num=rnorm(40),
                     gp1=rep(LETTERS[1:2],2),
                     gp2=rep(letters[1:2],each=2))

And applying tapply to it:

tmp <- tapply(mydata$num, list(mydata$gp1, mydata$gp2), sum)

The result of tapply is a matrix, but you can treat it like a table and use as.data.frame.table to convert it. This does not rely on any additional packages.

as.data.frame.table(tmp)

The two different data structures look like:

> tmp
         a         b
A 8.381483  6.373657
B 2.379303 -1.189488
> as.data.frame.table(tmp)
  Var1 Var2      Freq
1    A    a  8.381483
2    B    a  2.379303
3    A    b  6.373657
4    B    b -1.189488

Upvotes: 1

Maiasaura
Maiasaura

Reputation: 32986

library(reshape2)
new_data <- melt(old_data, id.vars=1)

Look into ?melt for more details on syntax.

example:

> df <- data.frame(x=1:5, y1=rnorm(5), y2=rnorm(5))
> df
  x         y1         y2
1 1 -1.3417817 -1.1777317
2 2 -0.4014688  1.4653270
3 3  0.4050132  1.5547598
4 4  0.1622901 -1.2976084
5 5 -0.7207541 -0.1203277
> melt(df, id.vars=1)
   x variable      value
1  1       y1 -1.3417817
2  2       y1 -0.4014688
3  3       y1  0.4050132
4  4       y1  0.1622901
5  5       y1 -0.7207541
6  1       y2 -1.1777317
7  2       y2  1.4653270
8  3       y2  1.5547598
9  4       y2 -1.2976084
10 5       y2 -0.1203277

Upvotes: 4

Related Questions