1
votes
G1G2Effect  WEIGHT2    Sim1    Sim2    Sim3    Sim4    Sim5    Sim6    Sim7    Sim8    Sim9   Sim10   Sim11   Sim12
1    -0.0312 2.833103 -0.0312 -0.0312  0.0000 -0.0312  0.0312  0.0000 -0.0312 -0.0312 -0.0312 -0.0312  0.0000 -0.0312
2    -0.0640 2.536790  0.0000 -0.0640 -0.0640 -0.0640 -0.0640 -0.0640 -0.0640 -0.0640 -0.0640 -0.0640 -0.0640 -0.0640
3     0.0420 3.309074  0.0420  0.0000  0.0000  0.0420  0.0420  0.0420  0.0420  0.0420  0.0420  0.0420  0.0420  0.0000
4     0.0332 2.476224  0.0332  0.0332  0.0332  0.0332  0.0332  0.0000  0.0332  0.0332  0.0332  0.0332 -0.0332  0.0332
5     0.0000 2.265289  0.0302  0.0302  0.0302  0.0000  0.0302  0.0000  0.0302 -0.0302  0.0302  0.0000  0.0302  0.0000
6     0.0000 1.272609  0.0116  0.0000  0.0000  0.0116  0.0000 -0.0116  0.0032 -0.0102  0.0101 

I have the data.frame above^. I am trying to make a new data.frame that is just the sum of each column.

df2 <- colSums(as.numeric(as.character(df1)))

but I am getting the error message

Error in colSums(as.numeric(as.character(exam))) : 'x' must be an array of at least two dimensions In addition: Warning message: In is.data.frame(x) : NAs introduced by coercion

What is going on?

1
If the columns are factor, use lapply to do this lapply(df1, function(x) as.numeric(as.character(x))) - akrun
Are those factors?? - Strange - Sotos
Did you try colSums (x, na.rm = FALSE)? - sairaamv

1 Answers

1
votes

The as.numeric/as.character works on vector/matrix and not on data.frame. We need to loop through the dataset and convert it to numeric and then apply the sum

sapply(df1, function(x) sum(as.numeric(as.character(x)), na.rm = TRUE))

We can also do this with tidyverse

library(tidyverse)
df1 %>%
    mutate_all(funs(sum(as.numeric(as.character(.)), na.rm = TRUE)))

If we really need colSums, one option is to convert the data.frame into matrix, so the factor class gets converted to character, then change it to numeric, assign the dim to the dimension of original dataset and get the colSums

colSums(`dim<-`(as.numeric(as.matrix(df1)), dim(df1)), na.rm = TRUE)