在r中执行mutate和case_when时出现“未定义的选定列”错误

rjee0c15  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(135)

我试图从一个数据框中提取值以在另一个数据框中使用,并得到以下错误

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

下面是我使用的语法

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

这是测试的数据

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"))

下面是val_1的dput

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)

以及val_2的dput

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)

看起来因为val_1和val_2都有一个列名,它导致case_when命令失败。如果我将以下值分配给val_1和val_2,那么case_when就可以正常执行。

val_1 <- -51.5617
val_2 <- -48.39284

有人能提供一些指导或更好的方法吗?

polhcujo

polhcujo1#

根据结构,它是一个具有单个列值的data.frame。

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

我们可以使用unlist[[来提取值-case_when检查类型。

test %>% 
  mutate(Value = case_when(row_number() == 1 ~ val_1[[1]],
                                      row_number() == 2 ~ val_2[[1]]))
  • 输出
# 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

相关问题