This is a test version of Biostars. For the public version, visit https://www.biostars.org.
dplyr mutate new columns
 gene    HSC_7256  HSC_6792  HSC_7653 HSC  SU048_Blast SU209_Blast

APOBEC3A   -0.9621548 0.000000 -1.235946  0.000000 0.8818193  -0.9603217
ATAD2B    5.6414714  5.402059 5.79380  5.755443  6.1243134  5.4867616

I m trying to get mean of these multiple columns like HSC one column and Blast another column where mean of each sample would be calculated.

Im doing this

j <- HSC_Blast_EPI_factor_gene %>% rowwise() %>% 
  mutate(HSC =mean(c(HSC_7256,HSC_6792,HSC_7653,HSC),na.rm = TRUE))

then I get a new column , but Im not able to pipe the same then when I try to do for the sample after HSC

When I doing this

j <- HSC_Blast_EPI_factor_gene %>% rowwise() %>% 
  mutate(HSC =mean(c(HSC_7256,HSC_6792,HSC_7653,HSC),
  mutate(LSC =mean(c(SU048_Blasts,SU209_Blasts))),na.rm = TRUE))

I get this error

> Error in mutate_impl(.data, dots) :    Evaluation error: argument
> ".data" is missing, with no default.

what am i doing wrong ?any suggestion or help would be appreciated

r

Why not melt the data frame (or gather) instead of moving onto the pain of rowwise operations? rowwise operations never really worked for me.

This is not answer to your question. However there is another way to do the same in R:

> test
      gene   HSC_7256 HSC_6792  HSC_7653      HSC SU048_Blast SU209_Blast
1 APOBEC3A -0.9621548 0.000000 -1.235946 0.000000   0.8818193  -0.9603217
2   ATAD2B  5.6414714 5.402059  5.793800 5.755443   6.1243134   5.4867616

output:

> cbind(test, HSC=apply(test[,grep ("hsc", ignore.case = T, names(test))],1,mean), SU=apply(test[,grep ("su", ignore.case = T, names(test))],1,mean))
      gene   HSC_7256 HSC_6792  HSC_7653      HSC SU048_Blast SU209_Blast        HSC         SU
1 APOBEC3A -0.9621548 0.000000 -1.235946 0.000000   0.8818193  -0.9603217 -0.5495252 -0.0392512
2   ATAD2B  5.6414714 5.402059  5.793800 5.755443   6.1243134   5.4867616  5.6481933  5.8055375

okay I will try your way ,i was just trying to see what is going wrong its a pretty simple operation...isn't it ..

dplyr solution without rowwise function:

> test
      gene   HSC_7256 HSC_6792  HSC_7653      HSC SU048_Blast SU209_Blast
1 APOBEC3A -0.9621548 0.000000 -1.235946 0.000000   0.8818193  -0.9603217
2   ATAD2B  5.6414714 5.402059  5.793800 5.755443   6.1243134   5.4867616

Command:

> test %>% mutate(HSC_mean = rowMeans(.[grep("HSC", names(.))]), SU_mean = rowMeans(.[grep("SU", names(.))]))

Output:

      gene   HSC_7256 HSC_6792  HSC_7653      HSC SU048_Blast SU209_Blast   HSC_mean    SU_mean
1 APOBEC3A -0.9621548 0.000000 -1.235946 0.000000   0.8818193  -0.9603217 -0.5495252 -0.0392512
2   ATAD2B  5.6414714 5.402059  5.793800 5.755443   6.1243134   5.4867616  5.6481933  5.8055375

Well, rowwise and two mutate functions are not necessary (in OP code). I guess for the second mutate function, it was looking for data to operate and could not find (IANP/S).

Your code has two issues:

  1. Column names mismatch between code and data frame (SU048_Blast vs SU048_Blasts)
  2. No need to use mutate function twice.

Your code should be:

j <- HSC_Blast_EPI_factor_gene %>%  rowwise() %>% mutate(HSC = mean(c(HSC_7256,HSC_6792,HSC_7653,HSC)), LSC = mean(c(SU048_Blast,SU209_Blast)))

thank you for rectifying me

Is it working now?

its working perfectly fine ..instead of comment if you could have answered then i would have vote up all the answers

0 answers

No answers yet.

Log in to answer this question.