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]]
Related
This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 8 months ago.
I want to store a column name in a variable and operate a dataframe based on that column name.
For example if a I have two columns named car_sales and airplane_sales. I have a variable var that a user sets to say car_sales. i then calculate a new column like so:
calc_col <- paste0(var,"_delta")
df$calc_col <- abs(df$var - lag(df$var ,12))
The var will change based on user input, so the resulting column will also change
How do I do this in R?
You could use:
df[[calc_col]] <- abs(df[[var]] - lag(df[[var]], 12))
This question already has answers here:
Why does apply convert logicals in data frames to strings of 5 characters?
(2 answers)
Selecting only numeric columns from a data frame
(12 answers)
Closed 2 years ago.
I know that the question is very easy, but I have a more specific one:
I have a data frame, with 50 variables (numeric and non-numeric) and 5000 observations.
Now what I want to do is create another data frame containing only the numerica variables of the original one.
On this website I found the solution of my problem, that is:
numeric_variables<-unlist(lapply(original_data,is.numeric))
X<-original_data[numeric_variables]
But I was wondering: why if I try like this, it does not work instead? what's wrong?
numeric_variables2<-apply(original_data,2,is.numeric)
x<-original_data[numeric_variables2]
try this :
names_num <- names(which(sapply(df, is.numeric)))
df_num <- df[, names_num]
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))
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',]
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]]