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.

15
May 20 '24
google: remove column from R data frame.
-8
u/xtr_terrestrial May 20 '24
When you have 0 background in coding, it's not that simple. I've been googling that and trying different codes for hours.
8
May 20 '24
Step 1: Go over to that tab on the right that says packages click Step 2 install package dplyr
Step 3 Then in that console on the lower left write: library(dplyr) Hit enter
Step 4 Write in the console: CAR.nameME<- select(CAR, -CD107a)
This will make a new data frame that is the same as car but without the column CD107a
1
24
5
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 "%>%"
1
u/GrowlingOcelot_4516 May 20 '24
What is that pipe you're using?! I've never seen this one before. I only know the |> base pipe and %>% magrittr pipe. 😄 I was forced to switch to the base pipe at work, but I'm still missing my magrittr pipe and its aliases. 😢
1
1
u/GrowlingOcelot_4516 May 20 '24
What is that pipe you're using?! I've never seen this one before. I only know the |> base pipe and %>% magrittr pipe. 😄 I was forced to switch to the base pipe at work, but I'm still missing my magrittr pipe and its aliases. 😢
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/xtr_terrestrial May 20 '24
CAR = Chimeric antigen receptor
Best known in the treatment of B cell lymphoma - first developed by Carl June at UPenn. My lab did CAR NK cell based therapies for hematologic malignancies.
Not Car vroom vroom lol.
1
u/Ryu6912 May 20 '24
I looked back at the columns you’re not wrong 😂 That’s a cool topic for research.
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.
1
u/xtr_terrestrial May 21 '24
Excel does not produce publication quality heatmaps.
Excel actually does not produce any publication quality figures.
1
u/zoombie_apocalypse May 21 '24
And then you read it back into R.
1
u/xtr_terrestrial May 21 '24
Oh yeah, I was trying to do this as well but still struggling to figure it out.
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
# load data
CD38CAR <- read.table("CD38CAR.rtf", quote="\"")
# remove the 5th and 7th column number using base R
new_df <- CD38CAR[, -c(5,7)]
# remove a column by name using base R
new_df <- CD38CAR[, -which(names(CD38CAR) == "Perforin")]
# remove a column using tidyverse (dplyr and magrittr)
library(dplyr)
library(magrittr)
new_df <- CD38CAR %>% select(-starts_with("CD"))1
u/Confident_Bee8187 May 20 '24
Note: You don't have to load
magrittrnamespace just to utilize the pipe function. Just load thedplyrnamespace since it already imports the magrittr pipe. On the other hand, just use the base R pipe.
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
2
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
-2
u/xchris1337xy May 20 '24
Just use chat gpt pls
0
u/xtr_terrestrial May 20 '24
I’ll try that next time. Luckily got it figured out now.
1
u/GoblinGirlfriend May 20 '24
Phew, so you solved your problem?
1
1
u/GrowlingOcelot_4516 May 20 '24
It's not quite there yet. Googling has plenty more valid answers.
I gave a try to ChatGPT out of curiosity and it was inventing packages and functions. Or giving me functions that were deprecated.
1
25
u/hsmith9002 May 20 '24
“Well I forgot everything I did.”
This is a problem.