Z SCORE by groups

I want to do the z-score by groups in R.

This is my code

data<-read.csv("C:/Users/usuario/Desktop/Tandas/TANDA JAPON/metabolomics/data/fwdreandres/R_PCA_tapR_sini.csv",sep=";",row.names = 1)

head(data)

#picture below is an example as variable names are too long

r%20problem

data<-scale(data,center = TRUE, scale = TRUE)

This code compute z-score for all the column, but not grouping by groups

Could you help me?

Many thanks,

Andrew

Hi @Andresecheve,

Try this:

library(dplyr)

# Replaces original variable
data <- 
  data %>%
  group_by(group) %>%
  mutate_all(scale)

# Creates new "var_scaled" variables
data <- 
  data %>%
  group_by(group) %>%
  mutate_all(list(scaled = scale))

For completeness, if you don't want to scale() every variable, you can choose a select few...

data <- 
  data %>%
  group_by(group) %>%
  mutate_at(vars(var1, var2, var4), scale)

data <- 
  data %>%
  group_by(group) %>%
  mutate_at(vars(var1, var2, var4), list(scaled = scale))
1 Like

Thank you @mattwarkentin for your quick response:

This is what I get:

data<-
data %>%
group_by(group)
Error in data %>% group_by(group) : could not find function "%>%"
mutate_all(scale)
Error in mutate_all(scale) : could not find function "mutate_all"

Any advice?

Thanks again

Instatll package tidyverse and dplyr, you are missing thses and that's why the error

Yes, as @Jitu has mentioned, you need to install the dplyr package:

install.packages('dplyr')

I have another problem,

As for R rows must be different names,

Excel data groups are MtrC, MtrMD

data<-read.csv("C:/Users/usuario/Desktop/Tandas/TANDA JAPON/metabolomics/data/fwdreandres/R_PCA_tapR_sini.csv",sep=";",row.names = 1)
head(data)

#see R data is MtrC1, MtrC2, MtrC3, ...

I put columns into rows

data<-t(data)
head(data)

Then I have this code problem:

data<-

  • data %>%
  • group_by(group)
    Error in UseMethod("group_by_") :
    no applicable method for 'group_by_' applied to an object of class "c('matrix', 'double', 'numeric')"

mutate_all(scale)
Error in UseMethod("tbl_vars") :
no applicable method for 'tbl_vars' applied to an object of class "function"

Could you help me?

Many thanks

Andres

When you transpose your data you have coerced it to a matrix. The code I provided won't work on a matrix, it is meant for a data.frame or tbl_df. You need to make your data a data frame again with as.data.frame() or as_tibble(). My preference is for as_tibble().

And why R reads groups as Mtr.C1, Mtr.C2,.... (those are samples for groups Mtr.C)?

Thus, when I do as_tibble() what I get is no gropus (1,2,3,4...),

Also "group" name for column one is lost

Yes, when you transpose your data with t() you can lose important information because a matrix does not adhere to the same principles as a data frame. I advise against using t() on your data. There are other ways to re-structure your data.

Do you believe your data is being read incorrectly into R? What does it look like in Excel? Perhaps there is a parsing issue. It is difficult to offer specific advice without knowing/having access to the data.

I have tried to transpose in the raw data but I do have other problems

Could you help me if I give you the data and my code?

Many thanks

Sharing the data would go a long way to helping troubleshoot your issue. You can send it to me privately if you aren't willing/allowed to share it publicly.

Can I send it to you privately via this forum?

Many thanks

Andres

Yes, click my picture/icon and then the envelope icon.

Mayby I can't because I am new?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.