Rsky
December 13, 2021, 7:45am
1
How to specify a column name as a non-character in a function like tidyverse::select.
I'm trying to create a function.
I want to select a specific column in a function.
data %>%
mutate(fct=factor(col_name)) %>%
pull(col_name)
I have tried to create my own function, but I get an error.
ex <- function(data,select) {
data %>%
mutate(fct=factor(col_name)) %>%
pull(col_name)
}
ex(data,col_name)
> x object 'col_name' not found
ex(data,"col_name")
> length(f) == length(.x) is not TRUE
How can I fix it?
Ideally, the column names should be recognized without enclosing them in double quotation marks, as in select().
ex(data,col_name)
thank you!
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
mtcars %>%
mutate(fct=factor(cyl)) %>%
pull(cyl)
#> [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
ex <- function(DATA,SELECT) {
DATA %>%
mutate(fct=factor(SELECT)) %>%
pull(SELECT)
}
ex(mtcars,"cyl")
#> [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
I have a question even about your original intent, before you move to contain it in a function.
You convert col_name to a factor called fct, and return only the original col_name. It seems therefore that the entire mutate is superfluous, a calculation for no purpose ?
Rsky
December 13, 2021, 10:44am
4
@nirgrahamuk
@technocrat
There was a spelling mistake.
I have created pseudo-data based on your advice.
data(ames)
my_data <- ames %>%
mutate(chr_Lot_Frontage = as.character(Lot_Frontage))
pull_column <- function(data,col){
data %>%
mutate(int=as.integer(col)) %>%
mutate(col_name = fct_reorder(col,int)) %>%
pull(col_name)
}
pull_column(my_data,chr_Lot_Frontage)
This will result in an error.
Do you know the solution?
Error: Problem with `mutate()` column `int`.
i `int = as.integer(col)`.
x object 'chr_Lot_Frontage' not found
pull_column(my_data,chr_"Lot_Frontage")
Error: Problem with `mutate()` column `col_name`.
i `col_name = fct_reorder(col, int)`.
x length(f) == length(.x)
this code can move collect.
my_data %>%
mutate(int=as.integer(chr_Lot_Frontage)) %>%
mutate(col_name = fct_reorder(chr_Lot_Frontage,int)) %>%
pull(col_name)
library(tidyverse)
library(tidymodels)
#> Registered S3 method overwritten by 'tune':
#> method from
#> required_pkgs.model_spec parsnip
data(ames)
my_data <- ames %>%
mutate(chr_Lot_Frontage = as.character(Lot_Frontage))
pull_column <- function(data,col){
data %>%
mutate(int=as.integer({{col}})) %>%
mutate(col_name = fct_reorder({{col}},int)) %>%
pull(col_name)
}
pull_column(my_data,chr_Lot_Frontage)
#> [1] 141 80 81 93 74 78 41 43 39 60 75 0 63 85 0 47 152 88
#> [19] 140 85 105 85 0 0 0 65 70 70 26 21 21 21 53 24 24 24
#> [37] 102 98 83 94 95 90 79 70 100 44 110 105 61 41 36 100 43 43
#> [55] 67 0 63 0 0 108 60 59 98 92 58 56 73 92 72 75 100 84
#> [73] 76 70 0 50 70 55 50 0 81 70 70 68 88 65 0 75 0 39
#> [91] 107 85 88 25 39 30 30 24 24 0 0 0 24 0 57 68 30 40
#> [109] 0 80 0 80 0 0 80 78 80 80 0 77 90 88 0 0 80 98
#> [127] 68 68 120 50 55 80 80 78 75 80 0 137 70 70 0 70 70 73
#> [145] 0 73 87 80 60 60 119 70 68 65 60 85 74 78 64 0 96 60
#> [163] 75 71 60 87 81 80 70 60 55 56 56 69 60 47 60 50 50 69
#> [181] 0 68 60 60 60 50 100 90 60 60 60 53 0 50 50 50 53 50
#> [199] 52 52 51 50 57 60 52 110 70 76 0 100 72 60 65 0 60 54
#> [217] 72 72 75 0 70 0 65 80 65 0 86 0 80 0 94 60 0 0
#> [235] 124 85 65 68 50 75 83 44 0 60 77 83 87 80 73 64 64 94
#> [253] 64 90 82 80 82 0 70 38 0 75 68 80 0 44 75 48 0 0
#> [271] 48 70 65 67 68 75 60 89 65 94 64 80 52 60 68 67 66 45
#> [289] 50 0 60 51 50 78 78 66 100 85 35 35 100 40 58 50 60 66
#> [307] 66 66 44 85 74 129 0 0 0 88 73 73 92 80 85 89 85 93
#> [325] 94 0 0 31 36 21 21 21 21 50 0 76 70 63 68 76 74 74
#> [343] 85 75 88 0 60 42 0 28 40 61 57 57 74 60 59 0 0 58
#> [361] 0 65 0 0 99 0 63 80 124 85 94 48 85 0 0 80 0 0
#> [379] 80 80 82 80 0 70 75 0 0 0 78 85 78 0 60 0 61 0
#> [397] 0 60 60 70 70 60 21 24 21 21 21 21 0 24 24 24 24 24
#> [415] 24 24 24 24 65 0 96 110 104 105 108 110 98 95 96 95 94 97
#> [433] 105 107 95 129 59 87 87 76 77 102 74 107 85 90 79 103 110 96
#> [451] 70 47 34 34 80 34 100 117 44 48 129 48 48 36 63 57 149 122
#> [469] 53 51 43 43 43 43 71 0 59 62 0 61 0 0 34 0 0 0
#> [487] 61 60 42 62 0 0 0 64 82 174 106 0 98 79 0 90 79 0
#> [505] 52 0 74 86 78 78 85 76 75 85 72 75 75 90 72 72 112 85
#> [523] 75 85 84 85 65 65 85 75 62 68 65 80 63 63 96 76 67 63
#> [541] 63 60 61 43 88 65 70 75 50 70 0 75 60 60 63 71 0 0
#> [559] 0 0 80 60 65 0 0 32 40 30 0 0 34 34 35 24 0 61
#> [577] 110 80 0 78 0 0 0 0 0 80 0 80 75 0 80 78 70 62
#> [595] 80 70 80 0 0 60 65 70 0 0 85 100 85 115 0 0 0 80
#> [613] 85 68 50 0 128 90 80 80 80 80 65 92 0 80 85 100 120 0
#> [631] 73 70 0 66 80 66 70 70 80 120 74 76 53 79 67 60 75 80
#> [649] 60 75 107 78 60 50 53 60 60 80 52 60 60 60 90 90 60 60
#> [667] 81 83 77 91 62 90 64 0 80 60 60 60 60 60 71 70 60 80
#> [685] 70 60 60 76 75 80 60 0 60 68 57 52 90 90 57 60 63 60
#> [703] 60 56 60 50 50 62 50 50 50 60 33 60 50 90 65 70 60 121
#> [721] 0 50 60 98 60 60 60 72 144 0 55 50 0 50 50 50 50 60
#> [739] 50 60 51 51 51 51 51 51 50 0 60 57 50 68 92 50 57 52
#> [757] 0 41 60 86 60 50 60 60 0 0 54 60 70 80 70 75 65 0
#> [775] 0 74 0 88 88 0 0 0 65 0 0 0 0 0 73 68 68 50
#> [793] 50 62 0 42 60 60 60 74 66 79 90 75 85 80 120 64 64 64
#> [811] 64 64 64 64 64 0 0 0 84 79 61 65 71 68 77 122 68 64
#> [829] 95 64 78 79 0 0 0 82 65 65 43 65 112 60 80 78 68 70
#> [847] 68 67 79 67 65 75 60 0 76 61 0 0 36 60 60 70 90 0
#> [865] 0 0 0 70 0 90 73 0 70 0 70 70 75 0 0 85 65 63
#> [883] 64 85 67 75 67 64 75 80 72 86 35 50 64 70 50 56 74 65
#> [901] 70 0 67 45 60 70 55 60 60 102 43 65 64 51 60 57 52 53
#> [919] 53 60 0 130 80 80 54 0 70 75 90 78 80 100 24 24 24 24
#> [937] 0 0 0 59 50 60 105 60 44 109 75 48 43 71 75 72 75 0
#> [955] 82 0 150 89 83 113 79 73 130 0 125 0 75 85 85 75 100 32
#> [973] 0 21 62 21 83 21 21 21 50 62 0 0 70 85 62 0 70 87
#> [991] 0 85 72 65 59 59 0 53 57 41 45 41 39 100 73 0 0 0
#> [1009] 40 65 101 77 53 0 60 0 46 63 78 0 56 85 0 90 0 80
#> [1027] 75 0 104 80 80 80 0 60 65 68 63 0 34 21 21 21 21 21
#> [1045] 21 21 53 24 24 0 96 96 114 67 98 107 105 104 108 96 102 74
#> [1063] 85 106 110 85 92 130 112 58 65 135 62 63 89 48 48 36 36 59
#> [1081] 0 0 43 43 53 80 62 59 51 0 0 55 93 71 60 0 41 77
#> [1099] 84 80 84 0 136 97 0 91 96 91 81 91 92 86 75 74 75 78
#> [1117] 91 73 80 87 72 72 95 75 75 85 85 65 85 62 64 63 68 67
#> [1135] 63 66 66 65 81 65 50 65 43 65 75 0 76 50 55 70 0 75
#> [1153] 65 60 65 95 70 0 105 59 35 37 30 30 34 24 24 0 0 42
#> [1171] 35 50 24 24 79 24 24 36 38 22 32 0 0 103 0 80 0 85
#> [1189] 80 0 75 90 73 72 65 72 74 87 0 0 76 0 0 0 74 77
#> [1207] 0 80 100 82 80 75 85 82 90 72 80 0 0 0 56 50 50 80
#> [1225] 80 80 80 63 0 80 75 80 101 90 0 71 70 74 82 76 90 72
#> [1243] 80 75 75 60 102 0 95 60 57 71 80 76 60 45 60 60 60 60
#> [1261] 70 78 76 0 0 313 74 60 60 70 80 80 70 60 64 59 60 113
#> [1279] 60 60 71 80 69 56 30 60 57 80 60 60 60 63 63 63 81 60
#> [1297] 60 60 44 50 100 75 62 50 50 103 87 69 100 53 34 69 60 60
#> [1315] 60 60 60 35 50 0 75 60 60 65 81 70 52 55 0 50 0 50
#> [1333] 50 59 50 50 50 50 50 50 60 99 52 0 52 52 51 51 60 60
#> [1351] 50 64 60 60 0 55 57 0 63 0 60 60 0 60 62 0 49 60
#> [1369] 60 60 60 82 55 115 80 62 0 70 80 0 68 57 0 0 0 60
#> [1387] 60 0 0 94 88 0 0 85 60 0 63 0 0 83 82 73 0 124
#> [1405] 50 85 75 85 66 120 68 50 50 50 70 68 60 60 86 0 80 0
#> [1423] 85 65 65 91 91 59 0 0 79 65 75 57 0 0 68 68 57 93
#> [1441] 88 80 75 68 0 65 0 0 0 40 88 60 60 75 75 0 0 73
#> [1459] 75 0 0 0 0 65 41 72 96 70 37 0 75 83 70 65 75 65
#> [1477] 64 85 85 74 63 0 0 0 0 67 67 75 75 64 65 63 90 60
#> [1495] 0 72 60 123 313 35 65 65 92 74 56 60 57 68 70 60 59 62
#> [1513] 67 67 45 55 0 60 60 60 55 51 64 66 79 63 63 52 54 51
#> [1531] 0 60 74 0 0 79 100 0 0 70 56 0 24 24 50 60 40 0
#> [1549] 71 58 75 60 75 80 60 50 52 120 60 64 44 44 44 0 0 0
#> [1567] 76 75 76 74 0 42 0 74 71 107 73 73 81 75 47 93 85 82
#> [1585] 0 85 79 85 97 78 77 32 150 0 0 41 21 21 0 21 21 100
#> [1603] 59 37 60 60 60 75 74 0 0 85 85 95 95 0 0 0 57 56
#> [1621] 41 0 0 63 63 60 0 0 0 0 160 0 0 38 35 86 98 52
#> [1639] 195 61 60 85 81 0 0 78 78 93 61 92 0 0 79 80 80 80
#> [1657] 128 70 90 0 64 80 70 0 0 63 66 61 70 0 0 60 34 33
#> [1675] 26 21 21 21 24 24 0 24 24 65 96 90 93 91 93 110 107 107
#> [1693] 110 105 109 107 110 105 98 110 118 59 134 76 82 86 94 99 99 110
#> [1711] 93 0 70 71 92 61 34 34 41 34 34 44 48 60 68 48 48 59
#> [1729] 65 65 58 62 63 88 65 73 73 73 72 64 64 53 53 0 53 182
#> [1747] 65 87 59 59 62 0 63 0 0 62 76 0 58 59 160 100 0 0
#> [1765] 89 74 83 104 92 95 82 65 0 88 82 75 91 84 49 76 98 116
#> [1783] 75 85 79 83 85 74 76 70 75 70 75 72 75 72 81 75 75 112
#> [1801] 84 84 84 100 85 102 63 63 60 65 70 0 65 0 50 50 0 60
#> [1819] 64 80 70 0 85 40 84 0 0 0 0 44 0 43 0 85 45 80
#> [1837] 32 47 40 30 35 35 24 34 0 24 0 0 24 66 73 0 106 50
#> [1855] 80 0 90 40 80 80 94 0 0 78 80 0 130 108 80 78 0 88
#> [1873] 80 80 70 0 79 0 0 0 0 0 80 65 70 74 0 0 76 70
#> [1891] 80 80 85 80 0 0 0 75 70 57 85 50 50 51 50 100 80 95
#> [1909] 79 79 63 0 100 70 78 0 73 65 60 70 75 80 90 70 80 72
#> [1927] 72 0 70 75 60 70 60 68 66 71 60 55 70 70 75 0 60 60
#> [1945] 0 113 60 55 60 60 60 60 93 75 66 70 60 60 78 60 60 60
#> [1963] 70 0 70 70 80 80 70 80 69 60 66 60 60 50 56 60 56 60
#> [1981] 60 120 50 50 50 50 57 40 60 0 53 60 35 85 56 90 60 75
#> [1999] 60 40 90 60 52 52 76 55 55 98 70 50 50 60 51 0 52 52
#> [2017] 51 51 51 52 60 47 60 60 60 60 0 60 40 40 0 57 120 60
#> [2035] 50 60 60 52 60 40 0 60 55 60 107 138 60 59 75 75 62 61
#> [2053] 65 0 70 99 0 70 86 0 66 109 71 0 81 99 0 70 91 85
#> [2071] 0 0 0 97 0 0 84 102 84 70 60 0 42 58 39 58 60 50
#> [2089] 60 104 114 116 86 103 82 75 77 77 71 93 91 103 66 69 68 85
#> [2107] 74 64 75 73 64 78 78 0 0 0 0 0 155 0 65 105 57 0
#> [2125] 60 70 60 69 47 43 68 0 72 72 97 59 72 45 39 75 70 120
#> [2143] 60 60 80 0 0 70 70 65 66 73 0 0 90 80 36 73 65 65
#> [2161] 70 79 63 65 64 64 64 70 75 67 73 74 80 81 65 0 0 74
#> [2179] 60 60 128 130 35 64 74 75 52 70 62 68 70 60 60 60 60 60
#> [2197] 55 55 54 51 60 63 53 55 53 53 40 53 50 82 82 60 126 86
#> [2215] 78 110 0 79 70 61 110 75 0 0 0 24 0 35 0 0 70 85
#> [2233] 50 50 50 50 50 46 59 60 75 64 75 65 60 59 44 0 0 71
#> [2251] 80 44 0 0 82 46 0 115 149 67 68 64 89 67 42 44 0 0
#> [2269] 73 80 0 85 0 50 93 85 88 70 200 62 94 100 0 44 44 21
#> [2287] 0 21 21 21 21 0 72 0 61 68 50 54 124 69 0 76 65 63
#> [2305] 62 0 85 0 0 90 60 0 0 0 54 70 134 50 50 72 42 168
#> [2323] 68 0 0 63 30 44 0 59 60 63 85 82 82 92 66 0 100 60
#> [2341] 69 90 91 0 0 0 0 80 81 79 80 85 0 75 81 78 78 78
#> [2359] 80 0 71 0 60 26 24 24 21 21 21 21 21 21 21 53 65 68
#> [2377] 24 24 129 72 111 110 107 108 120 98 101 108 120 80 82 85 103 82
#> [2395] 82 90 86 121 131 72 56 74 0 43 48 48 40 61 48 0 65 65
#> [2413] 65 58 58 102 105 96 75 43 0 0 0 0 0 43 43 43 53 53
#> [2431] 60 0 74 59 106 63 0 0 0 58 0 66 82 84 83 118 0 78
#> [2449] 93 83 114 0 92 0 75 49 49 85 73 85 72 100 81 84 65 65
#> [2467] 65 60 62 60 74 91 63 63 63 65 74 70 65 70 50 50 0 0
#> [2485] 60 70 70 68 0 88 0 65 0 0 0 0 65 0 100 121 105 0
#> [2503] 0 89 34 37 30 24 24 30 0 35 0 0 114 60 30 0 0 36
#> [2521] 40 40 68 79 102 0 90 0 80 78 80 75 80 72 78 70 70 0
#> [2539] 80 71 0 0 70 70 80 80 0 85 75 69 80 60 0 68 51 51
#> [2557] 52 92 80 89 80 89 79 79 82 0 0 85 88 75 174 109 70 70
#> [2575] 70 80 0 125 69 72 72 0 70 75 66 75 55 65 80 85 118 70
#> [2593] 94 50 50 60 90 60 60 60 60 60 60 60 60 60 84 85 83 77
#> [2611] 77 66 79 60 73 80 86 0 47 60 60 60 60 70 60 0 60 75
#> [2629] 70 70 69 69 74 70 70 62 57 60 60 60 60 58 66 70 56 56
#> [2647] 50 50 60 40 42 50 50 52 56 49 48 69 60 60 70 63 65 60
#> [2665] 60 84 90 33 65 63 0 65 69 50 65 52 0 51 51 52 52 51
#> [2683] 50 50 58 50 90 60 59 153 60 60 50 50 60 60 60 60 82 120
#> [2701] 100 92 55 50 50 75 72 70 0 96 80 75 0 74 0 78 0 0
#> [2719] 75 0 88 102 0 0 0 0 87 81 72 100 0 0 75 62 78 0
#> [2737] 114 60 78 80 70 75 75 57 77 50 0 88 80 73 133 64 64 64
#> [2755] 86 73 73 90 78 78 91 78 78 80 0 0 62 80 57 95 65 0
#> [2773] 43 70 79 78 68 72 95 65 118 55 50 42 69 120 70 60 0 90
#> [2791] 0 45 0 0 70 0 67 0 90 90 75 70 37 37 120 70 41 70
#> [2809] 74 70 84 89 65 68 65 65 67 134 67 38 35 73 62 75 90 35
#> [2827] 35 35 24 24 24 24 32 56 72 62 58 60 60 60 45 45 60 60
#> [2845] 76 0 60 60 58 43 96 69 60 54 54 84 51 60 66 0 80 80
#> [2863] 86 81 80 70 70 90 80 78 90 0 50 50 61 50 50 75 60 69
#> [2881] 50 50 60 41 44 69 65 70 78 140 82 42 0 0 0 54 50 0
#> [2899] 0 85 95 88 88 125 78 32 32 90 41 41 42 58 0 21 21 80
#> [2917] 21 21 21 21 21 55 63 80 160 37 0 62 77 74
#> 129 Levels: 0 21 22 24 25 26 28 30 31 32 33 34 35 36 37 38 39 40 41 42 43 ... 313
Created on 2021-12-13 by the reprex package (v2.0.1)
Look at rlang
:
1 Like
system
Closed
January 3, 2022, 12:20pm
6
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.