First of all apologies if I am not reporting correctly my problem, this is my first post within the R community. I am experiencing problems trying to apply a function to a whole data frame (rr) which I then save with a different name (rro). I have tried with a "for" loop, but it returns an empty list. I have also tried with apply, without success. Could anyone help?
Thanks,
Jorge
#> Attempt 1: returns an error: Error in FUN(left, right) : non-numeric argument to binary operator
R_code <-
rro <- rr
func = function(x) summarise_if(rr, is.numeric, 1 + ((rr - 1) * 2))
rro <- apply(rr, MARGIN = c(1,2), FUN = func)
#> Attempt 2: returns an empty list
R_code <-
rro <- rr
rro <- for(j in 1:ncol(rro)) {
if (is.numeric(rro[, j])) {
for(i in 1:nrow(rro)) {
rro[i,j] = 1 + ((rro[i,j] - 1) * 2)
}
}
}
You want to use mutate_if and have the function operate on the column, not on the data frame.
library(dplyr)
df <- data.frame(A = c("A", "B", "C"), B = c(1,2,3), C = c(10, 11, 31))
df
#> A B C
#> 1 A 1 10
#> 2 B 2 11
#> 3 C 3 31
df2 <- mutate_if(df, is.numeric, function(x) ((x - 1) * 2) + 1)
df2
#> A B C
#> 1 A 1 19
#> 2 B 3 21
#> 3 C 5 61
To point out why you're not getting the desired output with the for loop, you are assigning rro to it. You shouldn't do that. Inside the loop,you are updating the elements, and that's what you need. Removing just that part will do the job. Here's an illustration using the dummy data FJCC created.
rr <- data.frame(A = c("A", "B", "C"),
B = c(1,2,3),
C = c(10, 11, 31))
rro <- rr
for(j in 1:ncol(rro))
{
if (is.numeric(x = rro[, j]))
{
for(i in 1:nrow(x = rro))
{
rro[i, j] <- (1 + ((rro[i, j] - 1) * 2))
}
}
}
rro
#> A B C
#> 1 A 1 19
#> 2 B 3 21
#> 3 C 5 61
Much appreciated you corrected my loop, it helps me to understand a bit better how it works. However, when I run the code it returns the same matrix with no calculations. Did it work when you tried?
Yes sorry, I meant data.frame.
I managed to solve the issue. Apparently there was an odd issue in the line if (is.numeric(x = rro[, j]))
For some reason, R was not finding any column as numeric. I ran str(rro) and I could see how I had numeric columns. I removed that line and replaced the sequence in the "for" loop to the columns that I know are numeric and it works. Thanks!
Also thanks for the link to the minimal reproducible example, I will definitely have it in mind for next posts.