I've got a question here:
Create a dummy dataframe ' df_test' of 10 rows comprising of column (1) ' INT1' with sample values between the range 1 and 10
- Create a function ' check_even' that accepts two arguments
(1) ' df' - a dataframe
(2) ' col_test' - name of column to test if each row value in the indicated column is even or odd.
(3) ' col_multiply' - name of the column that stores the result of 'col_test' multiply by 2 if the 'col_test' is even and multiply by 3 if the 'col_test' is odd
(4) the function ends by returning the entire df with the results
- Test your function by running this code ' check_even(df_test, 'INT1', 'res')'
Here are my codes:
df_test = data.frame(INT1 = (sample(x = c(1:10),size = 10, replace = F)))
df_test
check_even = function(df, col_test, col_multiply) {
df$res = ifelse(df[col_test,] %% 2 == 0, df[,col_multiply] * 2, df[,col_multiply] * 3)
return(df)} #wrong
#run code
check_even(df_test, 'INT1','res')
In the code above, I get:
I'd also tried another way to do this using dplyr:
df_test = data.frame(INT1 = (sample(x = c(1:10),size = 10, replace = F)))
df_test
library(dplyr)
check_even = function(df, col_test, col_multiply){
df %>%
mutate(res = ifelse({{col_test}} %% 2 == 0, {{col_multiply}} * 2, {{col_multiply}} * 3))
}
check_even(df_test, 'INT1', 'res')
However, I'm still getting the error :
Error in mutate(., res = ifelse({ : Caused by error in
"INT1" %% 2
: ! non-numeric argument to binary operator
How can I solve this question? Thank you in advance.
CodePudding user response:
Base R
Here is the base R function corrected.
df_test = data.frame(INT1 = (sample(x = c(1:10),size = 10, replace = FALSE)))
#df_test
check_even = function(df, col_test, col_multiply) {
df[[col_multiply]] = ifelse(df[[col_test]] %% 2 == 0, df[[col_test]] * 2, df[[col_test]] * 3)
return(df)
}
#run code
check_even(df_test, 'INT1','res')
#> INT1 res
#> 1 4 8
#> 2 5 15
#> 3 1 3
#> 4 8 16
#> 5 2 4
#> 6 9 27
#> 7 3 9
#> 8 7 21
#> 9 10 20
#> 10 6 12
Created on 2022-06-04 by the reprex package (v2.0.1)
dplyr
solution
library(dplyr)
check_even = function(df, col_test, col_multiply){
df %>%
mutate({{col_multiply}} := ifelse({{col_test}} %% 2 == 0, {{col_test}} * 2, {{col_test}} * 3))
}
check_even(df_test, INT1, res)
CodePudding user response:
I think you need this, if I am not wrong you should change your logic:
Instead of passing the result column as an argument you could calculate it withn the function body:
library(dplyr)
check_even = function(df, col_test){
df %>%
mutate(res = ifelse({{col_test}} %% 2 == 0, {{col_test}} * 2, {{col_test}} * 3))
}
check_even(df_test, INT1)
INT1 res
1 10 20
2 3 9
3 6 12
4 4 8
5 7 21
6 9 27
7 5 15
8 2 4
9 1 3
10 8 16