Dave S
Dave S

Reputation: 53

R transposing dataframe with no column headings

I used count(case..) to group ages in sql and ended up with the following dataframe:

0-10    11-16   17-20   21-30   31-40   41-50   51-60   61-70   over70  age_unknown
60        285     161     368     476     453     247     101       62          114

I want to transpose this and add column headings 'age range' and 'number' but as far as I have been able to understand the reshape function needs headings to exist at the start, so I'm not sure how to go about this. Many thanks.

Upvotes: 1

Views: 561

Answers (3)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193637

What you might be looking for is stack. Using the data from Gavin's answer:

> stack(df)
   values         ind
1      60        0-10
2     285       11-16
3     161       17-20
4     368       21-30
5     476       31-40
6     453       41-50
7     247       51-60
8     101       61-70
9      62      over70
10    114 age_unknown

Upvotes: 1

Gavin Simpson
Gavin Simpson

Reputation: 174853

If that is a 1 row data frame like this:

df <- data.frame(matrix(c(60, 285, 161, 368, 476, 453, 247, 101, 62, 114), 
                        nrow = 1))
names(df) <- c("0-10", "11-16", "17-20", "21-30", "31-40", "41-50",
               "51-60", "61-70", "over70", "age_unknown")
df

> df
  0-10 11-16 17-20 21-30 31-40 41-50 51-60 61-70 over70 age_unknown
1   60   285   161   368   476   453   247   101     62         114

Then a simple manipulation will create the data frame in the format you want:

df2 <- data.frame(age_range = names(df), number = as.numeric(df[1, ]))
df2

> df2
     age_range number
1         0-10     60
2        11-16    285
3        17-20    161
4        21-30    368
5        31-40    476
6        41-50    453
7        51-60    247
8        61-70    101
9       over70     62
10 age_unknown    114

A simpler method might be to transpose df using t() and then fix up the result:

df3 <- t(df)
df3 <- cbind.data.frame(rownames(df3), df3)
rownames(df3) <- NULL
names(df3) <- c("age_range","number")
df3

> df3
     age_range number
1         0-10     60
2        11-16    285
3        17-20    161
4        21-30    368
5        31-40    476
6        41-50    453
7        51-60    247
8        61-70    101
9       over70     62
10 age_unknown    114
> str(df3)
'data.frame':   10 obs. of  2 variables:
 $ age_range: Factor w/ 10 levels "0-10","11-16",..: 1 2 3 4 5 6 7 8 10 9
 $ number   : num  60 285 161 368 476 453 247 101 62 114

Upvotes: 1

ndoogan
ndoogan

Reputation: 1925

If you have some continuous variable age and your desired cut points are known:

age <- rnorm(100,40,10)
cutpoints <- c(0,10,20,30,40,50,60,70,max(age))

fage <- table( cut(age, breaks=cutpoints) )

fage may in itself be all you need. But if you really want the data in a data frame:

df <- data.frame(age=names(fage), frequency=as.vector(fage))

Upvotes: 0

Related Questions