The sum
function will add all numbers together to produce a single number, not a vector (well, at least not a vector of length greater than 1).
It looks as though at least one of your columns is a factor. You could convert them into numeric vectors by checking this
head(as.numeric(data$col1)) # make sure this gives you the right output
And if that looks right, do
data$col1 <- as.numeric(data$col1)
data$col2 <- as.numeric(data$col2)
You might have to convert them into characters first. In which case do
data$col1 <- as.numeric(as.character(data$col1))
data$col2 <- as.numeric(as.character(data$col2))
It's hard to tell which you should do without being able to see your data.
Once the columns are numeric, you just have to do
data$col3 <- data$col1 + data$col2
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…