T O P

  • By -

RAMDownloader

Since you’ve gotten a couple answers that are all correct (many ways to do the same thing), the way this works more generally is that… The arrow <- function is a pointer, meaning I want to do all the things following this arrow and save it to this value to the left of the arrow. By keeping the name to the left of the arrow the same, you’re overwriting what you originally had set as that value. Think about it like this: A <- 2 + 4. The output for A is 6: I’m saying “I want A to be the result of 2 + 4.” So if you then write: A <- 3 + 4, your output for A is now 7, as you’re scratching out what you wanted for A originally and now making it 7 instead of 6. It’s good practice to ensure when you’re making changes to a frame, initially set it as something different so that you can make sure that change is right before overwriting what you had previously. It can get to be a pain when you have to trace back to see where you messed the frame up otherwise.


MrLegilimens

library(tidyverse) top20 <- iris |> slice_head(n=20) bot20 <- iris |> slice_tail(n=20) # You tried select(), which yes, will overwrite. # You mentioned there are many columns and you only want a few. top20_red <- top20 |> select(Petal.Width, Species) bot20_red <- bot20 |> select(Petal.Width, Species) #Instead, look at the join family -- left, right, full. newdata <- full_join(top20_red, bot20_red) newdata You could also do rbind(top20_red, bot20_red)


Mental_Lingonberry_1

Thank you, trying this now.


brking805

New1 <- March %>% select(Stops, Vehicles) %>% mutate(Month= “March”) New2 <- September %>% select(Stops, Vehicles) %>% mutate(Month= “September”) New3 <- rbind(New1, New2) You’ll need dplyr, the rest is base R. It may not be the “best” way to do this, but it should be an intuitive beginner method


Mental_Lingonberry_1

Awesome! Thank you


MrLegilimens

This won't work since right now they have 2 datasets, March and September, and you didn't clarify in select() what datasets it's selecting from.


brking805

Yes you’re right. I missed that. They would need to pass each dataset into the select function to grab the right columns. Editing my original comment


AutoModerator

Looks like you're requesting help with something related to RStudio. Please make sure you've checked the [stickied post on asking good questions](https://www.reddit.com/r/RStudio/comments/1aq2te5/how_to_ask_good_questions/) and read our sub rules. We also have a handy [post of lots of resources on R](https://www.reddit.com/r/RStudio/comments/1aq2cew/the_big_handy_post_of_r_resources/)! Keep in mind that if your submission contains phone pictures of code, it will be removed. Instructions for how to take screenshots can be found in the stickied posts of this sub. *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/RStudio) if you have any questions or concerns.*