Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
dplyr与ifelse突变如果为真,则得到什么';第x列中的s_R_If Statement_Dplyr - Fatal编程技术网

dplyr与ifelse突变如果为真,则得到什么';第x列中的s

dplyr与ifelse突变如果为真,则得到什么';第x列中的s,r,if-statement,dplyr,R,If Statement,Dplyr,我有一个名为place的数据框架,它有很多级别。我的目标是保持前十个级别,然后用“其他”替换所有其他级别 你得到这个结果是因为它是一个因素。如果更改为character,它将起作用。您还应使用place字符更新topten.area。但是,由于您正在使用dplyr,请查看n(),这将有助于解决您的问题我最近的评论中的错误,您的评论起到了帮助作用,这个问题得到了解决。非常感谢你 topten.area <- names(sort(table(raw.train$place), decreas

我有一个名为place的数据框架,它有很多级别。我的目标是保持前十个级别,然后用“其他”替换所有其他级别


你得到这个结果是因为它是一个因素。如果更改为character,它将起作用。您还应使用
place
字符更新
topten.area
。但是,由于您正在使用dplyr,请查看
n()
,这将有助于解决您的问题我最近的评论中的错误,您的评论起到了帮助作用,这个问题得到了解决。非常感谢你
topten.area <- names(sort(table(raw.train$place), decreasing = T)[1:10])
> topten.area
 [1] "Glasgow"      "Edinburgh"               "Aberdeen"    
 [4] "Dundee"     "Stirling"  "Inverness"                  
 [7] "Perth"                 "Aye"                 "Dingwall"                 
[10] "Avoch"

p.train <- raw.train %>%
  mutate(place = ifelse(place %in% topten.area, place, "other"))
> unique(p.train$place)
 [1] "other" "66"    "61"    "49"    "73"    "135"   "103"   "95"    "106"   "88"    "104"