sum two columns in R
I feel a bit embarrassed as I am trying to add two columns in R to get the product.
I have tried
sum(col1,col2)
but this returns
Error in Summary.factor(c(49L, 48L, 47L, 46L, 46L, 45L, 45L, 44L, 43L, :
sum not meaningful for factors
I thought this would very simple! both columns contain integers.
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
tablename$column3=rowSums(cbind(tablename$column1,tablename$column2),na.rm=TRUE)
This can be used to ignore blank values in the excel sheet.
I have used for Euro stat dataset.
This example works in R:
crime_stat_data$All_theft <-rowSums(cbind(crime_stat_data$Theft,crime_stat_data$Theft_of_a_motorised_land_vehicle, crime_stat_data$Burglary, crime_stat_data$Burglary_of_private_residential_premises), na.rm=TRUE)
You can use a for loop:
for (i in 1:nrow(df)) {
df$col3[i] <- df$col1[i] + df$col2[i]
}
#Two ways
df$New1 <- df$X1+df$X2+df$X3
#or choosing columns with X
df <-df %>% select(contains('X')) %>% mutate(New2=rowSums(.))