dplyr mutate new columns
0
0
Entering edit mode
7.3 years ago
1769mkc ★ 1.2k
 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 • 6.2k views
ADD COMMENT
1
Entering edit mode

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

ADD REPLY
1
Entering edit mode

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)))
ADD REPLY
0
Entering edit mode

thank you for rectifying me

ADD REPLY
1
Entering edit mode

Is it working now?

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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
ADD REPLY
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode

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).

ADD REPLY

Login before adding your answer.

Traffic: 2351 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6