How to delete specific columns in R [duplicate] - r

This question already has an answer here:
How do I delete columns in a dataframe if the name begins with X? [duplicate]
(1 answer)
Closed 3 years ago.
I have a data frame containing 2000 columns. Majority of the columns have "X111, X222 ,X123" and I want remove columns that starts with the name X

df[,-grep("^X",names(df)]
Grep logic looks for words starting (^) with X.

Related

How do I edit the values in my data frame to remove numerical values? [duplicate]

This question already has answers here:
Remove numbers at the beginning and end of a string
(3 answers)
How to remove beginning-digits only in R [duplicate]
(3 answers)
Closed last month.
I have a data frame that looks like this:
In the "x" column, I want to remove the number values and just have the string character, i.e. "Benign_freq" or "Recessive". Is there a way to edit these values using R?

How do I shorten row names in R? Please read below [duplicate]

This question already has answers here:
R - remove anything after comma from column
(5 answers)
Closed 2 years ago.
I have a table of values in R where the row names are very large. I want to shorten them. My row names look like this:
GSM1051550_7800246087_R02C01
I want to rename every row to only have the first part of the name, i.e., GSM1051550. How can I do this in R?
Building on jay.sf's comment (assuming your table's names is ABC):
row.names(ABC) <- sub("\\_.*", "", row.names(ABC))

How to drop multiple columns with similar names in a data frame [duplicate]

This question already has answers here:
How to drop columns by name pattern in R?
(6 answers)
Remove columns that contain a specific word
(5 answers)
Closed 3 years ago.
My data set has about 100 columns and some of them (about 25) are called similar but not them same. I'd like to delete all columns that start with "reads_per_million" because write this is so impractical:
data_tumor <- data_tumor[,-c(3,5,7,13,15,22,33,54,65,34,**etc,etc**)]
Thank you so much

Subsetting an R Matrix [duplicate]

This question already has answers here:
Extracting specific columns from a data frame
(10 answers)
Closed 4 years ago.
in R programming, how do I subset a matrix so that I can skip columns in between? I only know how to do it continuously such as 1:4, but what if I want the first, second, and fourth colum
You can select specific columns as follows:
new_df <- x[,c(1,2,4)]# Select column 1,2 and 4

Remove duplicates from a column R [duplicate]

This question already has answers here:
Remove duplicated rows
(10 answers)
Closed 7 years ago.
I have a long column (9500 rows in excel), where I have a lot of gene ids. I want to remove the duplicates.
ID
BXDC2
BXDC5
BXDC5
BZRPL1
BZRPL1
C10orf11
C10orf116
C10orf119
C10orf120
C10orf125
C10orf125
And I want the result to be:
ID
BXDC2
BXDC5
BZRPL1
C10orf11
C10orf116
C10orf119
C10orf120
C10orf125
Can anybody help me with an R script :-)?
You can use duplicated or unique. Here, I am assuming that the column name is 'ID'
df1[!duplicated(df1$ID),,drop=FALSE]
Or
library(data.table)#v1.9.4+
unique(setDT(df1), by='ID')

Resources