0

I have a dataframe with 21 columns. I want to merge 15 of these columns into one that can be identified as a variable 'Other.' This is my code so far, and I am not having luck in doing this so far, with rowSums it says

Error in as.data.frame(y) : argument "y" is missing, with no default 

And I don't know what else to try.

#Read in group abundances
 group.raw <- read.table("taxa_levels/phylum.txt", header=T, row.names=1, sep = "\t")
 group <- t(group)
 group <- sweep(group, 1, rowSums(group), '/') #Calculate relative abundance 
 group$Other <- rowSums(c("p__1", "p__2", "p__5", "p__6", "p__7", "p__8", "p__11", "p__12", "p__13", "p__14", "p__16", "p__17", "p__18", "p__19", "p__20"))

The data 'group' looks like this:

  row.names     p__1            p__2    
  1 X30841102   3.369851e-06    0.000000e+00    
  2 X49812105   0.000000e+00    0.000000e+00

 > str(group)
  num [1:152, 1:21] 3.37e-06 0.00 0.00 0.00 0.00 ...
  - attr(*, "dimnames")=List of 2
  ..$ : chr [1:152] "X30841102" "X49812105" "X26402102" "X25951102" ...
  ..$ : chr [1:21] "p__1" "p__2" "p__3" "p__4" ...
espop23
  • 77
  • 2
  • 2
  • 9
  • You are trying to `rowSums` across a vector of names. You need to do `rowSums(group[c("p__1","p__2")])` instead. – thelatemail Jul 18 '16 at 22:44
  • @thelatemail I tried that, and it gives this error now: 'Error in rowSums(phyla[c("p__1", "p__2", "p__5", : 'x' must be an array of at least two dimensions' – espop23 Jul 18 '16 at 22:47
  • You might have a matrix - therefore `rowSums(group[,c("p__1","p__2")])` - note the added comma to select columns. – thelatemail Jul 18 '16 at 22:48
  • can we see information on `str(group)` from just before you try the `rowSums()` command? – Ben Bolker Jul 18 '16 at 22:49
  • @thelatemail Yes it is a matrix. Trying that gives: 'Error in phyla[, c("p__1", "p__2", "p__5", : subscript out of bounds' – espop23 Jul 18 '16 at 22:50
  • @BenBolker: Here is it: '> str(group) num [1:152, 1:21] 3.37e-06 0.00 0.00 0.00 0.00 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:152] "X30841102" "X49812105" "X26402102" "X25951102" ... ..$ : chr [1:21] "p__1" "p__2" "p__3" "p__4" ...' Also added it to the question. – espop23 Jul 18 '16 at 22:51
  • No idea then. Using `group <- structure(c(3.37e-06, 0, 0, 0), .Dim = c(2L, 2L), .Dimnames = list( c("X30841102", "X49812105"), c("p__1", "p__2")))` which matches what you present exactly, the `rowSums(group[,c("p__1","p__2")])` code works fine. – thelatemail Jul 18 '16 at 23:16
  • That excerpt of `group`, is it before or after you transposed it? – catastrophic-failure Jul 19 '16 at 00:11
  • It was before. I have fixed it now, thanks! – espop23 Jul 19 '16 at 07:48

0 Answers0