Use dollar $ sign to access a column [duplicate] - r

This question already has answers here:
Dollar sign before a variable
(3 answers)
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 1 year ago.
This question may seem stupid, but I am curious about how to access a column by a column name with quote in data.frame, such as:
col.name <- colnames(feature)[i] # feature is a data frame, i is the index
feature$col.name # but this returns NULL

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 to delete specific columns in R [duplicate]

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.

how to extract a same name factor in many dataframe [duplicate]

This question already has an answer here:
How to use a string variable to select a data frame column using $ notation [duplicate]
(1 answer)
Closed 3 years ago.
I have 4 data frame:
temp2011, temp2012, temp2013, temp2014
These dataframe have same column name. (date, temp, humid, rain)
I want to extract a column like this:
temp2011$temp
temp2012$temp
temp2013$temp
temp2014$temp
It is very hassle, so I change the code like this:
col <- "temp"
temp2011$col
temp2012$col
temp2013$col
temp2014$col
but this is not work.
How can I do this problem.
Use [[]] instead of $ as temp2011[[col]]

R reference filtering column by variable name [duplicate]

This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 4 years ago.
Is there a way to get a filtered data frame, like this:
data[data$Measure=="Baseline",]
using a variable Name for Measure, i.e. measVarName == "Measure"?
Thanks.
Double bracket notation lets you select variables using a character string stored in a variable:
measVarName <- 'Measure'
data[data[[measVarName]] == 'Baseline',]

Subsetting columns by a string variable returns NULL in R [duplicate]

This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 5 years ago.
I can subset a column named A from a data frame x_data using x_data$A.
But if I do
some_string<-'A'
x_data$some_string
I get NULL.
Can someone please explain why is it so. Thanks.
You can reference a data frame with the $ operator using a string literal, only a column. If you want to subset using a string, use the list syntax:
sub_df <- x_data[[some_string]]

Resources