Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I refer to multiple columns in a dataframe expression?

Tags:

dataframe

r

Writing this is clunky:

df$a <- df$b + df$c

Is there a way to do (the equivalent of):

with df:
    $a <- $b + $c
like image 434
Reinderien Avatar asked Jun 18 '18 04:06

Reinderien


People also ask

How do I select multiple columns in a DataFrame Pyspark?

You can select the single or multiple columns of the DataFrame by passing the column names you wanted to select to the select() function. Since DataFrame is immutable, this creates a new DataFrame with selected columns. show() function is used to show the Dataframe contents.


1 Answers

We can use with command

df$a <- with(df, b + c)

Another option is using attach, which however is not recommended

attach(df)
df$a <- b + c

Another options with data.table and dplyr as suggested by @SymbolixAU

library(data.table)
setDT(df)[, a := b + c]

If you prefer dplyr chains.

library(data.table)
library(dplyr)

df %>%
  mutate(a:= b + c)

There is also forward pipe operator (%<>%) in magrittr package which doesn't require you to assign the object again to the variable.

library(magrittr)
df %<>%
  mutate(a = b + c)

Another one similar to with is using transform (suggested by @Maurits Evers)

df <- transform(df, a = b + c)
like image 196
Ronak Shah Avatar answered Nov 14 '22 08:11

Ronak Shah