• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: operating R dataframe using variables for the column name

Resolved: operating R dataframe using variables for the column name

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

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)) [/code]

The var will change based on user input, so the resulting column will also change
How do I do this in R?

Answer:

You could use:

df[[calc_col]] <- abs(df[[var]] - lag(df[[var]], 12)) [/code]

If you have better answer, please add a comment about this, thank you!

dataframe filter r
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: net::ERR_HTTP2_PROTOCOL_ERROR by http get request angular 15.2

02/04/2023

Resolved: How do I stop the command from happening if the requirements for it to work aren’t sert Discord.js

02/04/2023

Resolved: How to scroll bottom of div at launch?

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.