0

I have a following dummy data frame:

group <- c(rep("cat", 3),rep("dog", 3))
time <- c(rep(1:3,2))
mean <- c(78, 81, 83, 76, 83, 85)
  
df1 <- data.frame(group, time, mean)

And I would like to convert it to the following:

cat <- c(78, 81, 83)
dog <- c(76, 83, 85)
time <- c(1:3)

df2 <- data.frame(cat, dog, time)

I can do it in following way:

df2 <- df1%>% spread(group, mean)

How can I achieve this without using spread from tidyr?

ramen
  • 691
  • 4
  • 20

1 Answers1

1

You can use xtabs:

df1$group <- factor(df1$group)    
xtabs(mean ~ time + group, df1)

# time cat dog
# 1  78  76
# 2  81  83
# 3  83  85
AlexB
  • 3,061
  • 2
  • 17
  • 19