Applying a function to an entire data frame

#1

Dear all,

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)
  }
}
}
0 Likes

#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
2 Likes

#3

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

Created on 2019-03-02 by the reprex package (v0.2.1)

Hope this helps.

3 Likes

#4

Thank you, it works like a charm!

0 Likes

#5

Hi @Yarnabrina ,

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?

Thanks you,
Jorge

0 Likes

#6

It certainly worked on my device. And, I don't really understand why you're not getting results. It should've worked.

Can you please provide us a reproducible example of this problem?

If you don't know how to make one, here's a helpful post:

Just to note, for this example, rro is a data.frame, and not a matrix.

0 Likes

#7

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.

0 Likes

closed #8

This topic was automatically closed 7 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.

0 Likes