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))
Related
This question already has answers here:
Order dataframe in R [duplicate]
(1 answer)
Sort (order) data frame rows by multiple columns
(19 answers)
Closed last year.
I would like to reorder this dataframe based on the values of the "new" column, how can I do? tell me the exact formula, thanks
Substituting "DF" as your data frame, you can use this line of code:
DF[order(DF$new), ]
If you just wanted to view the data frame sorted, you can use
View(DF)
And click the arrows on the column name, which automatically sorts the data frame by ascending/descending.
This question already has answers here:
How can I divide one column of a data frame through another?
(2 answers)
Closed 3 years ago.
The below link shows my initial data frame.
The inc_per column has wrong values in it. so I want to recalculate this and update data frame with new values in it. So I tried this code:
cars<- within(cars,cars$per_inc<- (cars$oldprice / cars$newprice)*100)
But it created unnecessary columns and I didn't get my desired output
My desired output can be seen below:
it is very easy use
cars$per_inc <- (cars$oldprice / cars$newprice)*100
this will replace the current column values with new one.
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:
Split data frame string column into multiple columns
(16 answers)
Split different lengths values and bind to columns
(2 answers)
Closed 5 years ago.
I have a column of alphanumeric data containing data that looks like this K*01:01+K*01:02:01:01, K*77:01:08+K*01:02:01:22, K*10:01:77. I want to separate the data based on the strings before and after the + sign, new data should be displayed in 2 separate columns. I want my output to look like this:
column1 = K*01:01, K*77:01:08, K*10:01:77
column2 = K*01:02, K*01:02:01:02
I tried mydata$column1 <- sub("(.?)\+.", "\1", mydata$merged) works fine but when I used mydata$column2 <- sub(".\+(.?)", "\1", mydata$merged) for ID 3 K*10:01:77 is extracted both in columns 1 and 2, and I want column 2 to display a blank/empty cell for strings that do not have the + delimiter. Also I want the new columns to appear in the current data frame adjacent to the original merged column so packages like stringer do not work.
This question already has answers here:
Changing column names of a data frame
(18 answers)
Closed 8 years ago.
I'm a complete newbie and am trying to change column names of a dataset in R. For example, to change the column name of 'Eth' in dataset quine to 'Ethnic'. Any help or the name of the function is greatly appreciated.
The function 'colnames' is what you're looking for
let's say that 'Eth' is the third column, do
colnames(dataset)[3]<-"Ethnic"
colnames(dataset) returns exactly what you think it should, but you can also use it to set column names.
Doing the following
colnames(dataset)<-newColNames
where newColNames is a vector of names of the same length as the number of columns in dataset will change all the column names in order.
The following (as I did above)
colnames(dataset)[i]<-name
where name is a string and i is an integer, will change the name of the ith column to whatever the string "name" is