Recode and mutate_all in dplyr

I am trying to use recode and mutate_all to recode columns. However, for some reason, I am getting an error. I believe this post is similar to how to recode (and vice versa) variables in columns with dplyr , but the answer in this post used the lapply function.

Here is what I tried after reading dplyr package help pdf.

by_species<-matrix(c(1,2,3,4),2,2)
tbl_species<-as_data_frame(by_species)
tbl_species %>% mutate_all(funs(. * 0.4))
# A tibble: 2 x 2
     V1    V2
  <dbl> <dbl>
1   0.4   1.2
2   0.8   1.6

So this works well.

However, this does not work:

grades<-matrix(c("A","A-","B","C","D","B-","C","C","F"),3,3)
tbl_grades <- as_data_frame(grades)
tbl_grades %>% mutate_all(funs(dplyr::recode(.,A = '4.0')))

I get this error:

    Error in vapply(dots[missing_names], function(x) make_name(x$expr), character(1)) : 
values must be length 1,
but FUN(X[[1]]) result is length 3

Can someone explain what the problem is and why the code above does not work?

I would appreciate any help.

thank

+4
source share
2 answers

@Mir , . . ,

tbl_grades %>% mutate_all(funs(recode=recode(.,A = '4.0')))

, . , "",

dropnames<-function(x) {if(is(x,"lazy_dots")) {attr(x,"has_names")<-FALSE}; x}
tbl_grades %>% mutate_all(dropnames(funs(recode=dplyr::recode(.,A = '4.0'))))

.

tbl_grades %>% mutate_all(dropnames(funs(recode(.,A = '4.0'))))

dplyr ++ , , (, lag ), , ( dplyr::lag).

+6

dplyr::, .

funs(recode(., A = '4.0'))
<fun_calls>
$ recode: recode(., A = "4.0")

tbl_grades %>% mutate_all(funs(recode(. ,A = '4.0')))

# A tibble: 3 x 3
     V1    V2    V3
  <chr> <chr> <chr>
1   4.0     C     C
2    A-     D     C
3     B    B-     F

funs. , .

funs(dplyr::recode(., A = '4.0'))

Error in vapply(dots[missing_names], function(x) make_name(x$expr), character(1)) : 
values must be length 1, but FUN(X[[1]]) result is length 3

, :: . (. ?`::`). , , .

`::`(dplyr, recode)
function (.x, ..., .default = NULL, .missing = NULL) 
{
    UseMethod("recode")
}
<environment: namespace:dplyr>

dplyr::recode
function (.x, ..., .default = NULL, .missing = NULL) 
{
UseMethod("recode")
}
<environment: namespace:dplyr>

funs , as.character . , - . :

as.call(quote(recall(., A = '4.0')))
recall(., A = "4.0")

as.call(quote(recall(., A = '4.0')))[[1]]
recall

as.call(quote(recall(., A = '4.0')))[[2]]
.

as.call(quote(recall(., A = '4.0')))[[3]]
"4.0"

as.call(quote(recall(., A = '4.0')))[[4]]
Error in as.call(quote(recall(., A = "4.0")))[[4]] : 
  subscript out of bounds

, dplyr::recode, . , , .

as.call(quote(dplyr::recall(., A = '4.0')))
dplyr::recall(., A = "4.0")

as.call(quote(dplyr::recall(., A = '4.0')))[[1]]
dplyr::recall

as.call(quote(dplyr::recall(., A = '4.0')))[[1]][[1]]
`::`

as.call(quote(dplyr::recall(., A = '4.0')))[[1]][[2]]
dplyr

as.call(quote(dplyr::recall(., A = '4.0')))[[1]][[3]]
recall

, recode dplyr::.

as.call(quote(recall(., A = '4.0')))[[1]][[1]]
Error in as.call(quote(recall(., A = "4.0")))[[1]][[1]] : 
  object of type 'symbol' is not subsettable

, dplyr:: , , as.character , , .

as.call(quote(dplyr::recall(., A = '4.0')))[[1]] %>% as.character()
[1] "::"     "dplyr"  "recall"

funs , , , , , .

+3

Source: https://habr.com/ru/post/1650428/


All Articles