Create new column in r

In R, you can create a new column in a data frame using the mutate() function from the dplyr package. Here's an example:

library(dplyr)

# assume 'df' is your data frame
df <- data.frame(x = c(1, 2, 3), y = c(4, 5, 6))

df %>% mutate(z = x + y)

This will create a new column z in the data frame df with the values calculated by adding the values in columns x and y.

Alternatively, you can use the transform() function from the stats package:

transform(df, z = x + y)

This will also create a new column z in the data frame df with the same values.

You can also use the $ operator to create a new column:

df$z <- df$x + df$y

This will also create a new column z in the data frame df with the same values.

Note that in all cases, the new column will be added to the original data frame, and the original data frame will be modified. If you want to create a new data frame with the new column, you can use the mutate() function with the . operator, like this:

df_new <- df %>% mutate(z = x + y)

This will create a new data frame df_new with the new column z.