lundi 6 mai 2019

Is there a way to pickout data based on associations using R?

If I have a data set like this:

names <- c("Dave", "Ashley", "Drew")
score1 <- c(5, 1, 3)
opponent <- c("Drew", "Dave", "Ashley")
x <- cbind(names, score1, opponent)
x
y <- as.numeric(ifelse(x[, 3]==x[1, 1], x[1, "score1"], ifelse(
        x[, 3]==x[2, 1], x[2, "score1"], ifelse(
        x[, 3]==x[3, 1], x[3, "score1"], 1))))
y <- (y * score1)
x <- cbind(x, y)
x

Can I create a loop, to create a new column, where the number in the "score1" column is multiplied by the number from the "y" column from a different row. For instance, create a new column where the values at [1, 2] would be 5 since "Dave" had "Drew" in his row so "Dave"’s "score1" column “5” is multiplied by "Drew"’s "score1" column “3”. Is there a way to do this in a loop that would work for a hundred rows and a hundred columns? Currently, the only way I know to do it is to write a ton of "ifelse" statements like above.

Aucun commentaire:

Enregistrer un commentaire