Home > Software engineering >  Computing row-wise zscores in R dataframe
Computing row-wise zscores in R dataframe

Time:07-26

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.

Thanks!

CodePudding user response:

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))
  • Related