Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"Undefined columns selected" error when executing mutate and case_when in r

I am trying to extract values from one dataframe to use in another dataframe and am getting the following error

Error in `mutate()`:
! Problem while computing `Value = case_when(row_number() == 1 ~ val_1, row_number() == 2 ~ 
val_2)`.
Caused by error in `[.data.frame`:
! undefined columns selected

Here is the syntax that I am using

test %>% mutate(Value = case_when(row_number() == 1 ~ val_1,
                                      row_number() == 2 ~ val_2))

Here is the dput of test

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

structure(list(ID = c("47ae3cc00d8515d4cbab4704bf7e6b79", "47ae3cc00d8515d4cbab4704bf7e6b79", 
"47ae3cc00d8515d4cbab4704bf7e6b79", "47ae3cc00d8515d4cbab4704bf7e6b79"
), Book = c("MyBookie.ag", "MyBookie.ag", "Pinnacle", "Pinnacle"
), Home = c("Kansas St Wildcats", "Kansas St Wildcats", "Kansas St Wildcats", 
"Kansas St Wildcats"), Away = c("Michigan St Spartans", "Michigan St Spartans", 
"Michigan St Spartans", "Michigan St Spartans"), Team = c("Kansas St Wildcats", 
"Michigan St Spartans", "Kansas St Wildcats", "Michigan St Spartans"
), Price = c(-110, -110, -114, 100), Points = c(1.5, -1.5, 1.5, 
-1.5), BEP = c(0.523809524, 0.523809524, 0.53271028, 0.5), Fair_Price = c(100, 
100, -106.5420561, 106.5420561), Fair_BEP = c(0.5, 0.5, 0.515837104, 
0.484162896), Hold = c(0.045454545, 0.045454545, 0.031674208, 
0.031674208), Win = c(90.91, 90.91, 87.72, 100)), row.names = c(NA, 
-4L), spec = structure(list(cols = list(ID = structure(list(), class = 
c("collector_character", 
"collector")), Book = structure(list(), class = c("collector_character", 
"collector")), Home = structure(list(), class = c("collector_character", 
"collector")), Away = structure(list(), class = c("collector_character", 
"collector")), Team = structure(list(), class = c("collector_character", 
"collector")), Price = structure(list(), class = c("collector_double", 
"collector")), Points = structure(list(), class = c("collector_double", 
"collector")), BEP = structure(list(), class = c("collector_double", 
"collector")), Fair_Price = structure(list(), class = c("collector_double", 
"collector")), Fair_BEP = structure(list(), class = c("collector_double", 
"collector")), Hold = structure(list(), class = c("collector_double", 
"collector")), Win = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x00000153a4920d20>, 
class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

Here is the dput of val_1

structure(list(Fair_BEP = -51.5617029958564), class = "data.frame", row.names = c(NA, 
-1L))

val_1 <- (test[4,10] * test[2,11]) - (test[3,10] * 100)

and the dput of val_2

structure(list(Fair_BEP = -48.3928424591436), class = "data.frame", row.names = c(NA, 
-1L))

val_2 <- (test[3,10] * test[1,11]) - (test[4,10] * 100)

It appears that because val_1 and val_2 both have a column name it causes the case_when command to fail. If I assign the following values to val_1 and val_2 then the case_when executes just fine.

val_1 <- -51.5617
val_2 <- -48.39284

Can someone provide some guidance or a better way to go about this?

>Solution :

Based on the structure, it is a data.frame with a single column value.

> str(val_1)
'data.frame':   1 obs. of  1 variable:
 $ Fair_BEP: num -51.6

We could either unlist or use [[ to extract the value – case_when checks for the type.

test %>% 
  mutate(Value = case_when(row_number() == 1 ~ val_1[[1]],
                                      row_number() == 2 ~ val_2[[1]]))

-output

# A tibble: 4 × 13
  ID                               Book        Home               Away    Team  Price Points   BEP Fair_…¹ Fair_…²   Hold   Win Value
  <chr>                            <chr>       <chr>              <chr>   <chr> <dbl>  <dbl> <dbl>   <dbl>   <dbl>  <dbl> <dbl> <dbl>
1 47ae3cc00d8515d4cbab4704bf7e6b79 MyBookie.ag Kansas St Wildcats Michig… Kans…  -110    1.5 0.524    100    0.5   0.0455  90.9 -51.6
2 47ae3cc00d8515d4cbab4704bf7e6b79 MyBookie.ag Kansas St Wildcats Michig… Mich…  -110   -1.5 0.524    100    0.5   0.0455  90.9 -48.4
3 47ae3cc00d8515d4cbab4704bf7e6b79 Pinnacle    Kansas St Wildcats Michig… Kans…  -114    1.5 0.533   -107.   0.516 0.0317  87.7  NA  
4 47ae3cc00d8515d4cbab4704bf7e6b79 Pinnacle    Kansas St Wildcats Michig… Mich…   100   -1.5 0.5      107.   0.484 0.0317 100    NA  
# … with abbreviated variable names ¹​Fair_Price, ²​Fair_BEP
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading