Perhaps the simplest ways to solve it is to use dplyr/magrittr
or what is the same the metapackage tidyverse
:
df <- data.frame(NAMES=c("name1", "name1", "name1", "name2", "name2", "name3"),
EVENTS=c("event1", "event2", "event3", "event2", "event1", "event3"),
N=c(3, 6, 8, 2, 1, 4))
library(tidyverse)
# Expandimos los eventos en columnas y rellenamos con 0
df %>%
spread(key=EVENTS, value=N, fill = 0)
NAMES event1 event2 event3
1 name1 3 6 8
2 name2 1 2 0
3 name3 0 0 4
Eventually, if you would like to solve it with R base, it is quite simple too, using reshape()
although a little less clear to understand:
dfnew <- reshape(df, direction = "wide", idvar="NAMES", timevar="EVENTS")
dfnew[,-1][is.na(dfnew[, -1])] <- 0
- With
reshape(df, direction = "wide", idvar="NAMES", timevar="EVENTS")
we indicate that we are going to change from a long to a wide format ( direction = "wide"
). The column that we will not expand will be idvar="NAMES"
and the one that will be expanded will be timevar="EVENTS"
.
-
dfnew[,-1][is.na(dfnew[, -1])] <- 0
replaces the NA
only in the columns of the events.