I found this online and used this with my data:
df <- data.frame(seasons = c("Season1","Season2","Season3","Season4"))
for(i in unique(df$seasons)) {
df[[paste0(i)]] <- ifelse(df$seasons==i,1,0)
}
The only challenge is where there is a 0 in the resultant cell, I want to fill in a meaningful value from a data frame that has data arranged like so:
S1 | S2 | Value |
---|---|---|
Season1 | Season2 | 3 |
Season3 | Season1 | 5 |
Season2 | Season3 | 4 |
Note how a season in a pair could pop up at S1 or S2.
I'll need to fill for example,{row Season1; col Season 2} as well as {col Season 1 and row Season 2} in my matrix as 3.
Is there anyway for me to do this? I tried a few things but decided to give a shoutout to the community in case there is something simple out there I'm missing!
Thanks a bunch!
CodePudding user response:
There are three steps and decided to rebuild the original matrix and call it S:
# Make square matrix of zeros
rc <- length(unique(df[[1]]) ) # going to assume that number of unique values is same in both cols
S <- diag(1, rc,rc)
# Label rows and cols
dimnames(S) <- list( sort(unique(df[[1]])), sort( unique(df[[2]])) )
# Assign value to matrix positions based on values of df[[3]]
S[ data.matrix( df[1:2]) ] <- # using 2 col matrix indexing
df[[3]]
# -------
> S
Season1 Season2 Season3
Season1 1 3 0
Season2 0 1 4
Season3 5 0 1
It's now a real matrix rather than a dataframe.