Given an "empty" indicator dataframe:
Index Ind_A Ind_B
1 0 0
2 0 0
3 0 0
4 0 0
and a dataframe of values:
Index Indicators
1 Ind_A
3 Ind_A
3 Ind_B
4 Ind_A
I want to end up with:
Index Ind_A Ind_B
1 1 0
2 0 0
3 1 1
4 1 0
Is there a way to do this without a for loop?
I would do directly:
df = transform(df, Index=factor(Index, level=min(Index):max(Index)))
as.data.frame.matrix(table(df))
# Ind_A Ind_B
#1 1 0
#2 0 0
#3 1 1
#4 1 0
Data:
df = structure(list(Index = c(1, 3, 3, 4), Indicators = c("Ind_A",
"Ind_A", "Ind_B", "Ind_A")), .Names = c("Index", "Indicators"
), row.names = c(NA, -4L), class = "data.frame")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With