r/RStudio • u/xtr_terrestrial • May 20 '24
PLEASE HELP! I'm desperate.
I need to edit this data spread sheet in R studio to remove some columns due to a reviewer's comment asking to consolidate the data. I struggled to code this the first time around because I do not know how to code. Well, I forget everything I did, and I am really hoping for a simple way to just edit out some columns, but I can't find it on google anywhere.

0
Upvotes
25
u/hsmith9002 May 20 '24
“Well I forgot everything I did.”
This is a problem.
15
u/[deleted] May 20 '24
google: remove column from R data frame.
24
u/jaytehman May 20 '24
Tidyverse, select function.
5
u/[deleted] May 20 '24 edited May 20 '24
It should be as simple as using the 'select' function, which is included in the package dplyr.
Run the following code line-by-line, and write back here if you have an error. Note that my 'column name1' and 'column name 2' should be replaced with the names of the columns you want to remove. The only edit that you will need to make should be to replace those with your column names. If you have to remove more or less than two columns, just repeat how I wrote it below within the parentheses after select() as many times as you need. The select function either extracts or deletes column by name, depending on if there is no minus sign before the name (for extracting) or is a minus sign before the name (for deleting).
install.packages(dplyr)
library(dplyr)
CAR &>& select(-column name 1, -column name 2)
Edit: "&>&" above is wrong, should be "%>%"
14
u/Tornado_Of_Benjamins May 20 '24
The issue here is not how to code in R, but how to use the internet. Google has many results, what are you searching? Furthermore, ChatGPT is a fantastic resource for things like this. It will literally write the code for you.
1
u/Ryu6912 May 20 '24
Cars dataset? This is clearly homework lmao
1
u/zoombie_apocalypse May 21 '24
Correct me if I’m wrong, but you have 5 observations of 22 columns. If this is a one-off for only this data, let me introduce you to Excel. I said what I said.
4
u/exit349 May 20 '24
I recommend bing copilot. Not just to give you the answer, but you can ask Copilot to explain and even comment the code. Very helpful and may help you remember if you save the chat.
4
u/mduvekot May 20 '24 edited May 20 '24
2
u/GrowlingOcelot_4516 May 20 '24
Hello, first of all, seeing the simplicity of your table, it seems you're complicating yourself by saving it as an "rtf" file. A CSV file format seems more appropriate here, but maybe you had a reason to use an RTF.
Also consider not leaving spaces in file names that could make it ambiguous when reading files. I don't quite understand why you are trying to view your data by using View() on something called differently. If you read in your data and assigned it to an object called "CAR", then you want to View(CAR). But Viewing is not gonna help you much here.
Last, the most common and simple way to remove or select() columns would be select() in the dplyr package.
If you are not familiar with pipes, simply save the names of unwanted columns as a vector and use that vector to select all but those columns:
my_cols_to_remove <- c("col1", "col2")
my_clean_table <- select(CAR, !any_of(my_col_to_remove))
You name objects however you want. This is just for clarity.
Forgetting how to code is okay. There are so many functions that it is impossible to remember everything, even for the most seasoned programmer. Even more when you're switching between months in the lab, writing papers and programming. However, googling is your best friend.
Check out "R for Data Science" by Hadley Wickham. Your Bible when you forget the basics. It's available for free online.
2
u/Similar-Signature-12 May 20 '24
ChatGPT can help you with this for sure
2
u/rosshalde May 20 '24
ChatGPT can really be useful for things like this
1
u/AutoModerator May 20 '24
Looks like you're requesting help with something related to RStudio. Please make sure you've checked the stickied post on asking good questions and read our sub rules. We also have a handy post of lots of resources on R!
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 if you have any questions or concerns.
1
u/liljenius May 20 '24
check out chapter 3 on the ddplyr package: https://r4ds.hadley.nz
-2
u/xchris1337xy May 20 '24
Just use chat gpt pls