pecan icon indicating copy to clipboard operation
pecan copied to clipboard

Replace `transform()` with something programming-friendly

Open Aariq opened this issue 3 years ago • 4 comments

Actually, my advice would be to save this for a separate PR. I did a quick search and there are many instances of transform() in PEcAn which will all likely result in a similar problem of undefined global variables. It would probably easier to change them all at once when you've got the fix pattern figured out.

Originally posted by @Aariq in https://github.com/PecanProject/pecan/pull/2956#discussion_r921198756

transform() is similar to dplyr::mutate() but doesn't have a way of avoiding the 'undefined global variables' CRAN check error. The help file for transform() explicitly says to not use it in programming.

For example, transform(iris, Sepal.Length = Sepal.Length * 2) will result in an undefined global variable Sepal.Length. With dplyr's .data pronoun this can be avoided: dplyr::mutate(iris, Sepal.Length = .data$Sepal.Length * 2)

Aariq avatar Jul 14 '22 14:07 Aariq

related: https://github.com/PecanProject/pecan/issues/2758

Aariq avatar Jul 14 '22 14:07 Aariq

Hi @Aariq, I am not able to find any replacement for transform(). I think this Is the only function for changing data frames. What do you think?

nanu1605 avatar Jul 16 '22 10:07 nanu1605

Most uses of transform can be rewritten (sometimes much more wordily!) as explicit assignments to column names inside an explicitly named dataframe: new_data <- transform(data_with_tediously_long_name, some_column = some_other_column * 2) => new_data <- data_with_tediously_long_name; new_data$some_column <- new_data$some_other_column * 2

infotroph avatar Jul 16 '22 19:07 infotroph

new_df <- transform(old_df, new_col = old_col *2)

If a package already imports dplyr then you can do:

new_df <- dplyr::mutate(old_df, new_col = .data$old_col *2)

otherwise you'd have to do something like:

new_df <- old_df
new_df$new_col <- new_df$old_col * 2

Aariq avatar Jul 18 '22 22:07 Aariq