我试图从一个数据框中提取值以在另一个数据框中使用,并得到以下错误
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
有人能提供一些指导或更好的方法吗?
1条答案
按热度按时间polhcujo1#
根据结构,它是一个具有单个列值的data.frame。
我们可以使用
unlist
或[[
来提取值-case_when
检查类型。