Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Computing row-wise zscores in R dataframe

My dataframe contains numeric and character columns as shown below.

> df 
A   B   C   D E G
a1  b1  c1  1 2 3
a2  b2  c2  4 5 6
...

I want to compute row-wise zscores for numeric columns (D, E and G) using dplyr. I tried:

> df %>% rowwise() %>% mutate_if(is.numeric, ~scale(., center=T, scale=T))
> A   B   C   D E G
a1  b1  c1  NA NA NA
a2  b2  c2  NA NA NA

I’m unable to understand why NA is being returned. Any inputs on how to achieve what I want to do is greatly appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks!

>Solution :

Using base R

df[4:6] <- t(scale(t(df[4:6]), center = TRUE, scale = TRUE))

Or with tidyverse

library(dplyr)
library(tidyr)
library(purrr)
df %>% 
  mutate(out = pmap(across(where(is.numeric)), 
  ~ scale(c(...), center = TRUE, scale = TRUE)), .keep = 'unused') %>% 
  unnest_wider(out) %>% 
  mutate(across(D:G, c))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading