How do you add two columns together in R language to cr…
In R language, you can use the “+” operator to add two columns together into one column. Here is an example:
# 创建一个示例数据框
df <- data.frame(x = 1:5, y = 6:10)
# 将x列和y列相加成一列z
df$z <- df$x + df$y
# 输出结果
print(df)
This will generate:
x y z
1 1 6 7
2 2 7 9
3 3 8 11
4 4 9 13
5 5 10 15
In the example above, we used the “$” symbol to reference columns in the data frame, then used the “+” operator to add the x column and y column, and finally stored the result in a new column z.