Admire Tarisirayi Chirume
2021-Oct-18 11:26 UTC
[R] creating a new variable and merging it on the dataframe
Good day colleagues. Below is a csv file attached which i am using in my analysis. hh.id hd17.perm hd17employ health.exp total.food.exp total.nfood.exp 1 2 yes 1654 23654 23655 2 2 yes 2564 265897 65984 3 6 no 2547 123311 52416 4 8 no 5698 13648 12544 5 6 no 1254 36549 12365 6 8 yes 1236 236541 26522 7 8 no 4521 13264 23698 So I created a df using the above csv file as follows wbpractice <- read.csv("world_practice.csv") Now, I wanted to create a new variable called gap and scripted and executed the following command : wbpractice %>% mutate(gap = total.food.exp-total.nfood.exp) #gen a variable By recalling wbpractice, I could not see the new variable created. Running the command; names(wbpractice) shows the old variables only. Any help on how to append the newly created variable on my data? Alternative email: addtarris at icloud.com/TChirume at rbz.co.zw Skype: admirechirume Call: +263773369884 whatsapp: +818099861504>[[alternative HTML version deleted]]
PIKAL Petr
2021-Oct-18 11:34 UTC
[R] creating a new variable and merging it on the dataframe
Hi I cannot say anything about mutate but read.csv results in data frame you can use then wbpractice$gap <- with(wbpractice, total.food.exp-total.nfood.exp) Cheers Petr BTW, do not use HTML formating your email is a mess.> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Admire Tarisirayi > Chirume > Sent: Monday, October 18, 2021 1:26 PM > To: Jim Lemon <drjimlemon at gmail.com> > Cc: r-help mailing list <r-help at r-project.org> > Subject: [R] creating a new variable and merging it on the dataframe > > Good day colleagues. Below is a csv file attached which i am using in my > analysis. > > > > hh.id > > hd17.perm > > hd17employ > > health.exp > > total.food.exp > > total.nfood.exp > > 1 > > 2 > > yes > > 1654 > > 23654 > > 23655 > > 2 > > 2 > > yes > > 2564 > > 265897 > > 65984 > > 3 > > 6 > > no > > 2547 > > 123311 > > 52416 > > 4 > > 8 > > no > > 5698 > > 13648 > > 12544 > > 5 > > 6 > > no > > 1254 > > 36549 > > 12365 > > 6 > > 8 > > yes > > 1236 > > 236541 > > 26522 > > 7 > > 8 > > no > > 4521 > > 13264 > > 23698 > > > > > > So I created a df using the above csv file as follows > > wbpractice <- read.csv("world_practice.csv") > > Now, I wanted to create a new variable called gap and scripted andexecuted> the following command : > > wbpractice %>% > > mutate(gap = total.food.exp-total.nfood.exp) #gen a variable > > > > By recalling wbpractice, I could not see the new variable created.Running> the command; > > names(wbpractice) > > > > shows the old variables only. Any help on how to append the newly created > variable on my data? > > > Alternative email: addtarris at icloud.com/TChirume at rbz.co.zw > Skype: admirechirume > Call: +263773369884 > whatsapp: +818099861504 > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
Good day colleagues. Below is a csv file attached which i am using in my> analysis. > > > > household.id <http://hh.id> > > hd17.perm > > hd17employ > > health.exp > > total.food.exp > > total.nfood.exp > > 1 > > 2 > > yes > > 1654 > > 23654 > > 23655 > > 2 > > 2 > > yes > > NA > > NA > > 65984 > > 3 > > 6 > > no > > 2547 > > 123311 > > 52416 > > 4 > > 8 > > NA > > 2365 > > 13648 > > 12544 > > 5 > > 6 > > NA > > 1254 > > 36549 > > 12365 > > 6 > > 8 > > yes > > 1236 > > 236541 > > 26522 > > 7 > > 8 > > no > > NA > > 13264 > > 23698 > > > > > > So I created a df using the above and its a csv file as follows > > wbpractice <- read.csv("world_practice.csv") > > Now i am doing data cleaning and trying to replace all missing values with > the averages of the respective columns. > > the dimension of the actual dataset is; > > dim(wbpractice)[1] 31998 6 I used the following script which i executed by i got some error messages for(i in 1:ncol( wbpractice )){ wbpractice [is.na( wbpractice [,i]), i] <- mean( wbpractice [,i], na.rm = TRUE) } Any help to replace all NAs with average values in my dataframe?> >>[[alternative HTML version deleted]]