21

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.

brucezepplin
  • 9,202
  • 26
  • 76
  • 129
  • 1
    Please include the result of `dput(head(yourdata))` in your question. – Rich Scriven Sep 25 '14 at 19:43
  • 1
    The error message seems rather informative. If you don't know what a factor is in R, then you need to go back to your introductory text and do some more self study. – IRTFM Sep 25 '14 at 19:44
  • 1
    "I am trying to add two columns in R to get the product" - Huh? – joran Sep 25 '14 at 19:47
  • `with(mtcars, sum(factor(mpg), factor(cyl)))` versus `with(mtcars, sum(mpg, cyl))` should help you out a little – Rich Scriven Sep 25 '14 at 19:48

8 Answers8

44

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
blakeoft
  • 2,370
  • 1
  • 14
  • 15
  • 1
    How would you deal with NAs in one of those columns? Do you need to replace them with 0 before the addition or can you do this in one go? – Mariano Kamp Feb 19 '16 at 17:59
  • It depends on how you want to handle NAs. If you want them to be zero, you'll need to do that at any point before you do the addition. – blakeoft Nov 02 '16 at 16:59
3
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)  
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
2

You can use a for loop:

for (i in 1:nrow(df)) {
   df$col3[i] <- df$col1[i] + df$col2[i]
}
1

First way

df$New1 <- df$X1+df$X2+df$X3

Second Way

df <-df %>% select(contains('X')) %>% mutate(New2=rowSums(.))
Sandy
  • 1,100
  • 10
  • 18
0

It could be that one or two of your columns may have a factor in them, or what is more likely is that your columns may be formatted as factors. Please would you give str(col1) and str(col2) a try? That should tell you what format those columns are in.

I am unsure if you're trying to add the rows of a column to produce a new column or simply all of the numbers in both columns to get a single number.

Reuben Mathew
  • 598
  • 4
  • 22
0

You can do this :

    df <- data.frame("a" = c(1,2,3,4), "b" = c(4,3,2,1), "x_ind" = c(1,0,1,1), "y_ind" = c(0,0,1,1), "z_ind" = c(0,1,1,1) )
df %>% mutate( bi  = ifelse((df$x_ind + df$y_ind +df$z_ind)== 3, 1,0 ))
0
df$row_sums <- 0
# Loop over each row
for (i in 1:nrow(df)) {
  # Sum the values in each row starting from the second column
  row_sum <- sum(df[i, 2:ncol(df)])
  
  # Assign the row sum to the new column
  df$row_sums[i] <- row_sum
}
# Add up the row sums to get the total sum of all row sums
total_row_sum <- sum(df$row_sums)

# Print the total sum of all row sums
cat("The total sum of all row sums is:", total_row_sum, "\n")
umar
  • 21
  • 5
-1

Try this for creating a column3 as a sum of column1 + column 2 in a table

tablename$column3=rowSums(cbind(tablename$column1,tablename$column2))