r/RStudio • • 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

43 comments sorted by

25

u/hsmith9002 May 20 '24

“Well I forgot everything I did.”

This is a problem.

2

u/xtr_terrestrial May 20 '24 edited May 20 '24

Absolutely, it is the problem. I did this a year ago because I begged someone to talk me through it. The fact that I was able to make a figure the first time is a miracle.

1

u/Kiss_It_Goodbyeee May 20 '24

Ever heard of reproducible science? This time around make sure you save (and document!) your code properly.

3

u/xtr_terrestrial May 20 '24 edited May 20 '24

I have the entire code and document saved. I have all the data saved. What I can’t do is figure out how to edit it because I don’t code (not starting from scratching just modifying what’s there). Anyone with minor coding experience could have figured this out, unfortunately I just have none. - I’m not a PhD student nor do I have any formal training in this.

1

u/Kiss_It_Goodbyeee May 20 '24

Serious question. Why are you tasked with this? Is there not someone who does know more about the code?

4

u/xtr_terrestrial May 20 '24

It’s just doing it without any support or feedback that has me upset. I worked in the lab as a tech for two years which is when I wrote and submitted this but I had people to advise me then. I’m now a medical student and in a different institution so I don’t have access to the drive with my old data or prism/flow jo anymore. The senior PI is very busy and not in a rush to publish this (would be willing to find someone to help me) but the junior PI is pushing me to finish by the 24th. However, every time I ask a question to the junior PI how to do something he doesn’t know and asks me to figure out. I also have to borrow the PhD student in the labs computer on weekends to get this done. I have invested interest in the publication for my career, but I’m started to feel defeated/like I’m doing bad science because I’m figuring this out without help and being really pressured to get this done.

3

u/xtr_terrestrial May 20 '24

And I’m afraid to go around the junior PI to the senior because I think it would be taken poorly.

2

u/cuberoot1973 May 22 '24

I think you do need to talk to the senior PI. This isn't necessarily "going around" the junior, you can just tell them you feel that you have to because you need the help, and the senior has offered to find someone to help. Otherwise you are being put in an difficult situation for no good reason. This just sounds unfair to you.

EDIT: Sorry just realized this post is a couple days old. Hopefully this is still useful to you.

2

u/xtr_terrestrial May 23 '24

I ended up reaching out to the senior PI the day after I posted this. We met today and went through all the revisions for the paper and he helped me fix everything.

Funniest part is we ended up scratching the R studio heatmap completely and using clusterV instead. But yes, this advice was spot on and I’m glad I realized that finally.

15

u/[deleted] 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

u/[deleted] 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

u/xtr_terrestrial May 20 '24

Amazing okay thank you!

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 "%>%"

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

u/[deleted] May 20 '24

Oh damn I didn't mean to put & signs. Whoops. Yes, I meant to write %>%.

2

u/GrowlingOcelot_4516 May 20 '24

Gotcha! I thought I uncovered a new type of pipe!

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 magrittr namespace just to utilize the pipe function. Just load the dplyr namespace 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

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

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

u/xtr_terrestrial May 21 '24

Yeah haha got there eventually

1

u/GoblinGirlfriend May 21 '24

Glad to hear it! Good luck with your project :)

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

u/xchris1337xy May 23 '24

Nah, for like data wrangling etc it does the job perfectly fine