Watts-College / cpp-527-spr-2022

https://watts-college.github.io/cpp-527-spr-2022/
0 stars 1 forks source link

Final Project- Functions are returning correctly, but not updating data frame #32

Closed dholford closed 2 years ago

dholford commented 2 years ago

Hello,

I thought I had most of my functions for Part I of the final project nailed down, but when I went back in today to finalize it didn't seem to be working properly. My functions don't seem to be updating/adding the objects I'm returning in the global environment when I run the function.

For example, in the add_gender() function, when I run the function I get the table I'm expected, but nothing is changing in the global environment. So, when I move to the next function, I can't call the updated data I need.

`add_gender <- function(){

d$first.name <- get_first_name( Full.Name=d$Full.Name )

unique.first.names <- unique( d$first.name )

gen <- gender( unique.first.names )

d <- merge(d, gen, by.x = "first.name", by.y = "name")

d$gender[ is.na(d$gender) ] <- "uncoded" d$gender <- factor( d$gender, levels=c("male","female","uncoded") )

return(d)

}`

Which yields:

tables

But no change to d in the global environment:

environment

I tried adding the data as a specific argument in the function to then reference the data frame, but that still wasn't changing anything.

`add_gender <- function(Data){

Data$first.name <- get_first_name( Full.Name=Data$Full.Name )

unique.first.names <- unique( Data$first.name )

gen <- gender( unique.first.names )

Data <- merge(Data, gen, by.x = "first.name", by.y = "name")

Data$gender[ is.na(Data$gender) ] <- "uncoded" Data$gender <- factor( Data$gender, levels=c("male","female","uncoded") )

return(Data)

}

add_gender(d)`

I feel like it's probably a very simple answer/basic tenant of functions I'm just forgetting. But I went back through the Functions review/overview and I'm still struggling. I understand the scope of the function, but I thought referencing the object in the function would then allow the function to operate on the data in the global environment?

Thanks, Dylan

dholford commented 2 years ago

Again I think I found the answer, or at least part, to my own question. I would simply assign the object to the function. So in the above example:

d <- add_gender(d)

But if this first step is just creating the functions that will be saved in Utils.R, so will those assignments, separate from the functions still work when sourcing the Utils.R in part 2 to make sure the changes actually get passed through each step of the cleaning/wrangling process?

Dselby86 commented 2 years ago

You got it.

On Tue, Feb 22, 2022, 11:25 PM dholford @.***> wrote:

Again I think I found the answer, or at least part, to my own question. I would simply assign the object to the function. So in the above example:

d <- add_gender(d)

But if this first step is just creating the functions that will be saved in Utils.R, so will those assignments, separate from the functions still work when sourcing the Utils.R in part 2 to make sure the changes actually get passed through each step of the cleaning/wrangling process?

— Reply to this email directly, view it on GitHub https://github.com/Watts-College/cpp-527-spr-2022/issues/32#issuecomment-1048481567, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB4EHB47YTBOW23FL5CICXLU4R4VVANCNFSM5PDMSQIA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

dholford commented 2 years ago

duck