Reputation: 340
So my objective is to fill in the last column of a data frame with values. The data frame is [343,8]. I wrote up a solution to this but it is not working and I am curious as to why my idea would not work:
result = vector(length = 3)
for(i in 1:343){
for(j in 1:3){
result[j] = combos[i:j]
}
combos$prize[i] = score(result)
}
The name of the larger data frame is "combos", the last column of the data frame is "prize".
My attempt was to create the empty vector "result", fill it with the values from the first three columns of "combos", then apply a function that I created called "score" on to the "result" vector, and finally fill in the the specific entry in the "combo" data frame with the value obtained from "score".
My version doesn't work, but there is a solution in the textbook which works:
for (i in 1:nrow(combos)) {
symbols <- c(combos[i, 1], combos[i, 2], combos[i, 3])
combos$prize[i] <- score(symbols)
}
Now I don't see much difference between what the author did and myself. Yes he didn't use a second FOR loop, but the procedure to arrive at the final result seems consistent.
With that being said, what is it that I did not do correct?
EDIT This is a copy of the score function that I am calling in my for loop:
score = function(symbols) {
# identify case
same = symbols[1] == symbols[2] & symbols[2] == symbols[3]
bars = symbols %in% c("B","BB","BBB")
# get prize
if (same) {
payouts = c("DD" = 100, "7" = 80, "BBB" = 40, "BB" = 25, "B" = 10, "C" = 10,
"0" = 0)
prize = unname(payouts[symbols[1]])
} else if (all(bars)){
prize = 5
} else {
cherries = sum("C" == symbols)
prize = c(0,2,5)[cherries + 1]
}
# adjust for diamonds
diamonds = sum("DD" == symbols)
prize * 2 ^(diamonds)
}
Upvotes: 0
Views: 891
Reputation: 451
So your issue is with how you are creating your symbols "vector". Because a data.frame is really considered a type of "list" in R, what you are doing is assigning a 3-element list to your symbols variable. Hence, you will need to unlist this before you can make the comparisons you are seeking.
for (i in 1:nrow(combos)) {
symbols <- unlist(c(combos[i, 1], combos[i, 2], combos[i, 3]))
combos$prize[i] <- score(symbols)
}
This should solve your issue though I cannot exactly test without the combos data.frame but I assume this is your issue just looking at the code and the error message you received. Good luck!
Upvotes: 1
Reputation: 10223
Note that i:j
in combos[i:j]
returns a vector from i to j. Try to run 5:7
for example. So you're doing some 'weird' subsetting with combos[i:j]
. You probably want that to be combos[i, j]
which returns the (i, j)th entry of combos.
combos[i:j]
returns the i'th through j'th entries of combos
(which I assume is a numeric matrix). In R, matrices are stored in a column-major order, so the your intial subsetting returns the corresponding entries and might 'span the columns'. Take a look a the following to illustrate:
x <- matrix(1:6, 2, 3)
print(x)
# [,1] [,2] [,3]
#[1,] 1 3 5
#[2,] 2 4 6
print(x[2:3])
#[1] 2 3
print(x[2,3])
#[1] 6
So, does
result = vector(length = 3)
for(i in 1:343){
for(j in 1:3){
result[j] = combos[i, j]
}
combos$prize[i] = score(result)
}
work?
Upvotes: 1