dplyr relocate columns r function tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back cradle-to-grave our I am mark Jim grass and I'm gonna teach you today how to use the relocate two plier function it's very simple and I didn't actually get this idea out of my head I was browsing through the reddit our stats which if you haven't been there before you should definitely check it out I'll show you where I found this question from a user so it's under the reddit are stats which is an awesome resource for helping you with your AR and especially with statistics right so this particular user had a question about moving passenger ID to the left of survived now how he got it to the right of survived I don't know because I think I think naturally when you first download the data source it's actually the other way around I'll show you that in a moment but you know it seems like something very simple to do and surprisingly sometimes an are you know you you could do some googling and it takes you like two hours to figure out how to fix something that simple right so luckily for us there's been some good users and and the community has jumped in and gave the answer right there's multiple answers now I'm not going to walk you through all of these answers but the first one that really caught my eye was the relocate function that deployer has and so they're basically saying here's how you do it it's pretty simple and I'm gonna walk you through how to use that relocate function based on my our stats reddit resource so thanks to everybody on our stats for always chiming in and bringing up these awesome little features and functions but I want to show it to you in a program right now so let's just jump right to it if you have if you haven't heard of the Titanic you probably need to get out from under your rock but so there's a package called Titanic so just type in Titanic and you're install and just do Titanic here we're gonna install that data set so install.packages Titanic I already have it installed so I'm gonna cancel out but then you can just load that library Titanic right so let's just go ahead and zoom in on that we can do library Titanic and once you do that you'll you won't get anything but you can set up data data frame or a table to the Titanic data says so let's just do that let's just say my data is equal to and then we'll use scoping so we'll do Titanic Titanic : : and you can see I have a few different things here I have a test set in a train set which this will come in super handy when we're actually doing some testing and some training which I really hope we get to soon I've been slacking but let's just go ahead and use the test set and I'm just gonna hit the control enter on that you'll see on the right hand side I have my data is equal to 418 observations of 11 variables so there's 11 features or variables and 418 observations now as you can see up here hopefully you can see it it's passenger ID P class then name so we're just gonna do a quick example of how to move name to the left of passenger ID first so let's go ahead and do that so let me close that out and all we're gonna do is use the deploy function so again if you don't have to plier click on install and then just typing DP l yr d plier like a pair of pliers something you can manipulate stuff with right install that once you've done that let's just load that library as well and there we have it now you have to make sure it's version 10 point oh great or 1.0 1.0 I'm sorry 1 point now you can do that by if you already have it installed you can actually just type in package version with the I guess camel case and then in quotes you can type in deploy her and you should get version 1.0.0 if you don't have 1.0.0 it's probably not gonna work because I don't think the relocate function was added into 1.0.0 that's based on my reddit I just recently read the reddit comments and that's what I got from that so simple as that let's just go ahead and relocate so all we're gonna do is we're gonna say my data is equal to a relocated data set so relocate what are you gonna bring in your data so I'm gonna bring in my data and I want to relocate I want to relocate name to the left of passenger ID it's kind of the opposite of what this person wanted but since our name is to the right of it again I'll show you real quick so it's called name and I want to bring it to the left of passenger ID I want to bring it before passenger ID so going back we would just say I want to bring in my variable called passenger ID and I want to do dot don't forget there's a little period there dot before is equal to and I'm sorry passenger ID passenger ID in oh no quotes I'm sorry don't need the quotes but this is supposed to be named right here so I wanna bring name before passenger ID so let's do that train enter it looks like passenger ID doesn't exist it's because I have a capital D there so let's go ahead and make that a little case boom so now we go back to my data and we see the name is to the left of passenger ID see that was like a very simple way to do it right there's multiple ways you can use the relocate functions but that's one way to do it another way to do it is to use the after you can use there's an after instead of a before so if you want to move something after for example let's say I want to move sex after age so simple it's the same concept so we're just gonna copy but we're just gonna say my data is equal to relocate my data and we want sex to go dot after equals age and we should be able to run that and now we should have sex to the right of age which we do now one other things since we're here I'm gonna show you how to do it the tidy verse way so to speak so what we want to do is we want to say my data we're gonna let's bring back passenger ID to the right of name-o let's go let's put the name to the right of passenger ID again but this time we're going to do it a little bit differently I'm gonna say take my data and pipe it command do control shift M which will give you this pipe operator I want to pipe it into the relocate function and once you do that that my data which has to be a lowercase M so you guys need to catch me on these things my data is actually going to be the first input to this parameter you don't actually have to put it there but that's what the pipe and operator is doing essentially so my second input is I want to put the passenger ID I guess I don't know what do we have right now I'm a little bit lost so let's just put a name to the right of passenger ID back to where it was right so we're gonna say I want to put named after dot after equals after equals passenger ID come and enter on that so that's the tidy way you kind of have to get used to that notation but we had the name now back to where it was so what you usually do first and aesthetics is you usually after every pipe operator you would hit the enter key and you'd have more space for it because normally you'd want to be piping this into all kinds of stuff so we could pipe this into the view so when we run it we can actually now I've already just switched them so it's not gonna do anything but it does automatically open up the view for you but that's what the piping operator does I know we've talked about it before in previous tutorials but but as you can see not only did I pipe it in there now my data is empty see how I screwed this up I hope that you guys might have caught that because what I've done was I've actually set this piped into view right i piped it into the view and then that entire object which doesn't even make sense is stored as my data so that doesn't make sense so we can't store it that way but I can view it before I store it now that would make more sense so let me run this over again these lines right so now I can view it and say oh yeah that's correct and once that's correct then I can say oh I don't want to view it anymore I know it's correct we don't need to pipe it in there but I do have to store it somewhere I want to overwrite maybe my own data frame or Tibble whatever you want to call it at this point I think it's probably a Tibble but one way to figure that out remember let's just jump back in more more stuff one way to figure that out we can do structure of my data and it's gonna tell us straight-up what it is so if I scroll up a tad to the top you will see that it's a data frame so it's not a tipple but that's the idea I just wanted to show you that real quick so that was a very quick and simple way to move data around a little bit there's plenty of other ways to do this but I wanted to show you that particular method because it was referred to in the our stats reddit subreddit so that's where I post a lot of videos and I do appreciate them allowing me to post my videos on their subreddit because it is very popular so hashtag our stats on a lot of the stats stuff you do on Twitter and then find that subreddit and you'll usually find me posted and probably once every week or two on there so don't forget like subscribe and I will see you guys in the next video [Music] you
Info
Channel: CradleToGraveR
Views: 547
Rating: 5 out of 5
Keywords: dplyr, relocate, move columns in R, relocate columns in dataframe, move columns data frame R, rstudio, r studio, import data into r, read data in to r, copy data into r, loading data into r, copy data from excel to r, readxl, reading data from excel, importing data from excel, importing data with rstudio, r packages, r programming tutorial, r programming language, statistics with r, data science with r, statistics for data science, r programming for beginners, cradletograver
Id: uCt3Yz5rWFE
Channel Id: undefined
Length: 9min 50sec (590 seconds)
Published: Mon Jul 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.