kc2819
kc2819

Reputation: 1571

R transform data frame (group by)

I am an R and Stack Overflow newbie so hope I'm following protocol here.

How do I transform a data frame like:

FRUIT   NUMBER  NAME    AGE
apples  5       Joe     13
oranges 6       Joe     13
apples  2       Mary    10
oranges 4       Mary    10

into a grouped data fame like:

NAME    AGE  APPLES ORANGES
Joe     13   5       6
Mary    10   2       4

Thanks in advance.

Upvotes: 0

Views: 1376

Answers (2)

mpalanco
mpalanco

Reputation: 13570

Using sqldf:

library(sqldf)
sqldf('SELECT NAME, AGE,
      MAX(CASE WHEN FRUIT = "apples" THEN NUMBER ELSE NULL END) apples, 
      MAX(CASE WHEN FRUIT = "oranges" THEN NUMBER ELSE NULL END) oranges
      FROM d 
      GROUP BY NAME 
      ORDER BY NAME')

Output:

  NAME AGE apples oranges
1  Joe  13      5       6
2 Mary  10      2       4

Upvotes: 0

musically_ut
musically_ut

Reputation: 34288

You can use the function dcast from package reshape2.

> library('reshape2')
> d <- read.table(header=T, text="FRUIT   NUMBER  NAME    AGE
+ apples  5       Joe     13
+ oranges 6       Joe     13
+ apples  2       Mary    10
+ oranges 4       Mary    10");


> dcast(d, NAME + AGE ~ FRUIT, value.var='NUMBER')
  NAME AGE apples oranges
1  Joe  13      5       6
2 Mary  10      2       4

This is changing data from a (somewhat) long format to deep format, you can find more details of how to approach this task here.

Upvotes: 2

Related Questions