1

Below is my data

name  1day  2day  3day
a      20    30    40
b      30    10     0
c      12     5     3

In R, if i perform sum(mydata[2,]) then i am getting below error:

Error in Summary.factor(c(105L, 76L, 95L, 98L, 106L, 29L), na.rm = FALSE) : 
  ‘sum’ not meaningful for factors
  1. could you please tell me, how to get b total paid amount?
  2. how to remove the factors.

Also please suggest me other alternatives.

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • You have to convert factors to num eric if you want to manipulate your data without loss of information. Check [this](https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-integer-numeric-without-loss-of-information) out. – Adamm Apr 04 '18 at 07:31
  • 3
    Your first column is a factor. If you want to sum over the second *row*, you could do `sum(mydata[2,-1])` (in order to ignore the first column). If you want to sum over second *column*, you should put the index *after* the comma, i.e. `sum(mydata[,2])`. Also, in order to run over all the rows, you could do `rowSums(mydata[-1])` – David Arenburg Apr 04 '18 at 07:39

1 Answers1

2

We don't have to remove non numeric columns. We just need to make sure we aren't summing non numeric columns (character, factor, logical for example). sum(2, "c") will give us an error for this reason. If we have a column of logical type sum will treat FALSE as 0 and TRUE as 1. See ?sum for more info.

With base R as @DavidArenburg mentions, we can do:

df$row_sum <- rowSums(df[-1])
df
  name X1day X2day X3day row_sum
1    a    20    30    40      90
2    b    30    10     0      40
3    c    12     5     3      20

If we have other columns that aren't numeric and shouldn't be summed, we can use sapply to only sum rows of columns that are numeric:

df$row_sum <- rowSums(df[, sapply(df, is.numeric)])
df
  name X1day X2day X3day row_sum
1    a    20    30    40      90
2    b    30    10     0      40
3    c    12     5     3      20

This would be useful when removing the first column isn't enough. For example, if there is a forth column that is of character type.

@Jaap also mentioned another base R solution using Reduce:

Reduce('+', df[,-1])

# again, if removing the first column isn't enough we can do
Reduce('+', df[, sapply(df, is.numeric)])

Alternatively, with dplyr:

library(dplyr)

df %>% 
  rowwise() %>%
  mutate(row_sum = sum(X1day, X2day, X3day))
# A tibble: 3 x 5
  name  X1day X2day X3day row_sum
  <chr> <int> <int> <int>   <int>
1 a        20    30    40      90
2 b        30    10     0      40
3 c        12     5     3      20

Data:

df <- read.table(text = "name  1day  2day  3day
a      20    30    40
b      30    10     0
c      12     5     3", header = TRUE, stringsAsFactor = FALSE)
tyluRp
  • 4,678
  • 2
  • 17
  • 36
  • I'm not sure if the OP fully understood the issue. If you 'remove the factor' from the first column, it still will throw because it is a character. Your answer provides a solution but a bit more explanation might help. – sedot Apr 04 '18 at 08:42
  • Hi, One more question. a <- 85.37 102.42 85.97 104.59 72.60 97.21 b<- 2.07 58.36 53.55 70.96 49.11 66.52 c<- SHREYA, BHAWSAR, RAVI, MOHAN, GANDHI, KARTHIK When i use cbind to combine all vectors, then the names ie. c is getting as some numbers. how to get it as names like c a b shreya 85 3 bhawsar 102 58 please help – chaitanya yatam Apr 04 '18 at 10:47