user2543622
user2543622

Reputation: 6756

R convert a row into columns

I have data as below.

      Item.Nbr accuracy_level freq
1   82078-002          Worst    1
2   82078-007            Bad    1
3   82078-007          Worst   44
4   82078-007           <NA>   11
5   82078-007           Good   7

I want to convert the accuracy_level column into rows, like below

    Item.Nbr    Worst   Bad <NA>    Good
1   82078-002   1       0     0     0
2   82078-007   44      1     11    7

I dont know exactly how many unique entries are going to be in column accuracy_level

Upvotes: 0

Views: 101

Answers (3)

akrun
akrun

Reputation: 886938

A base R option using xtabs

xtabs(freq ~ Item.Nbr + accuracy_level, df)
#          accuracy_level
#Item.Nbr    Bad Good <NA> Worst
# 82078-002   0    0    0     1
# 82078-007   1    7   11    44

Or reshape from base R

 res <- reshape(df, idvar='Item.Nbr', timevar='accuracy_level', direction='wide')
 res[is.na(res)] <- 0
 res

Upvotes: 0

Thomas K
Thomas K

Reputation: 3311

Solution with tidyr:

df <- read.table(header = T, text = "Item.Nbr accuracy_level freq
1   82078-002          Worst    1
2   82078-007            Bad    1
3   82078-007          Worst   44
4   82078-007           <NA>   11
5   82078-007           Good   7")

library(tidyr)
df %>% spread(accuracy_level, freq, fill = 0)
#>    Item.Nbr <NA> Bad Good Worst
#> 1 82078-002    0   0    0     1
#> 2 82078-007   11   1    7    44

Upvotes: 1

Rich Scriven
Rich Scriven

Reputation: 99321

You could use the reshape2 package.

library(reshape2)
dcast(df, Item.Nbr ~ accuracy_level, fill = 0)
#    Item.Nbr Bad Good <NA> Worst
# 1 82078-002   0    0    0     1
# 2 82078-007   1    7   11    44

Data:

df <- structure(list(Item.Nbr = structure(c(1L, 2L, 2L, 2L, 2L), .Label = c("82078-002", 
"82078-007"), class = "factor"), accuracy_level = structure(c(4L, 
1L, 4L, 3L, 2L), .Label = c("Bad", "Good", "<NA>", "Worst"), class = "factor"), 
    freq = c(1L, 1L, 44L, 11L, 7L)), .Names = c("Item.Nbr", "accuracy_level", 
"freq"), class = "data.frame", row.names = c("1", "2", "3", "4", 
"5"))

Upvotes: 1

Related Questions