“对于哪个数字x
和y
,在十进制数字系统中表示为6x12y
的数字除以45
?”
当然,下面不是和我的同龄人讨论的解决方案,而是试图在R中测试我的技能,然而,最后一行并没有做我想做的事情。
library(tidyverse)
library(stringi)
replicate(2, 0:9, simplify = FALSE) %>%
expand.grid() %>%
as.tibble() %>%
transmute(newcol=do.call(paste0,list(6,Var1,12,Var2))) %>%
map_df(as.numeric) %>%
filter(newcol%%45==0) %>%
transmute(x_y=paste(stri_sub(newcol,c(2,5),c(2,5)),collapse = " "))
我用这个得到了想要的结果。但我在上一次的错误是什么呢?
replicate(2, 0:9, simplify = FALSE) %>%
expand.grid() %>%
as.tibble() %>%
transmute(newcol=do.call(paste0,list(6,Var1,12,Var2))) %>%
map_df(as.numeric) %>%
filter(newcol%%45==0) %>%
transmute(x_y=map2_chr(stri_sub(newcol,2,2),stri_sub(newcol,5,5),paste))
发布于 2017-10-02 08:13:59
你需要把你的行动划成一行。因此,在管道中添加rowwise()
条件将修复它,即
library(tidyverse)
replicate(2, 0:9, simplify = FALSE) %>%
expand.grid() %>%
as.tibble() %>%
transmute(newcol=do.call(paste0,list(6,Var1,12,Var2))) %>%
map_df(as.numeric) %>%
filter(newcol%%45==0) %>%
rowwise() %>% # <--- Added the rowwise
transmute(x_y=paste(stri_sub(newcol,c(2,5),c(2,5)),collapse = " "))
给出了预期的结果
资料来源:本地数据帧3 x 1组:#A tibble: 3 x 1 x_y 1 0 2 9 0 3 4 5
https://stackoverflow.com/questions/46520796
复制相似问题