Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace transform() with something programming-friendly #2961

Closed
Aariq opened this issue Jul 14, 2022 · 4 comments · Fixed by #3097
Closed

Replace transform() with something programming-friendly #2961

Aariq opened this issue Jul 14, 2022 · 4 comments · Fixed by #3097
Labels
Misc: good first issue Topic: CRAN Changes related to getting PEcAn CRAN ready

Comments

@Aariq
Copy link
Collaborator

Aariq commented Jul 14, 2022

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 #2956 (comment)

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 Aariq added the Topic: CRAN Changes related to getting PEcAn CRAN ready label Jul 14, 2022
@Aariq
Copy link
Collaborator Author

Aariq commented Jul 14, 2022

related: #2758

@nanu1605
Copy link
Collaborator

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?

@infotroph
Copy link
Member

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

@Aariq
Copy link
Collaborator Author

Aariq commented Jul 18, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Misc: good first issue Topic: CRAN Changes related to getting PEcAn CRAN ready
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants