[英]Normalize data per row in R
How can I scale/normalize my data per row (Observations)?如何缩放/标准化每行的数据(观察)? Something like [-1:1] like az score?
像 [-1:1] 这样的 az score 吗?
I have seen previous post which involve normalization of the whole dataset like this https://stats.stackexchange.com/questions/178626/how-to-normalize-data-between-1-and-1 , but id like to normalise per row so they can be plotted in a same box plot as they all show same pattern across x-axis.我看过以前的帖子,其中涉及整个数据集的标准化,例如https://stats.stackexchange.com/questions/178626/how-to-normalize-data-between-1-and-1 ,但我喜欢标准化每个行,因此它们可以绘制在同一个框 plot 中,因为它们在 x 轴上都显示相同的模式。
Obs <- c("A", "B", "C")
count1 <- c(100,15,3)
count2 <- c(250, 30, 5)
count3 <- c(290, 20, 8)
count4<- c(80,12, 2 )
df <- data.frame(Obs, count1, count2, count3, count4)
dff<- df %>% pivot_longer(cols = !Obs, names_to = 'count', values_to = 'Value')
ggplot(dff, aes(x = count, y = Value)) +
geom_jitter(alpha = 0.1, color = "tomato") +
geom_boxplot()
Based on the link you shared, you can use apply
to use the corresponding function to rescale dataframe over [-1,1].根据您共享的链接,您可以使用
apply
来使用相应的 function 在 [-1,1] 上重新调整 dataframe。
library(scales)
library(ggplot2)
library(tidyr)
Obs <- c("A", "B", "C")
count1 <- c(100,15,3)
count2 <- c(250, 30, 5)
count3 <- c(290, 20, 8)
count4<- c(80,12, 2 )
df <- data.frame(count1, count2, count3, count4)
df <- as.data.frame(t(apply(df, 1, function(x)(2*(x-min(x))/(max(x)-min(x)))- 1)))
df <- cbind(Obs, df)
dff<- df %>%
tidyr::pivot_longer(cols = !Obs, names_to = 'count', values_to = 'Value')
ggplot(dff, aes(x = count, y = Value)) +
geom_jitter(alpha = 0.1, color = "tomato") +
geom_boxplot()
Console output:控制台 output:
If you pivot it longer, you can group by your observations and scale:如果您的 pivot 它更长,您可以按您的观察和规模分组:
df %>%
pivot_longer(cols = !Obs, names_to = 'count', values_to = 'Value') %>% group_by(Obs) %>%
mutate(z=as.numeric(scale(Value))) %>%
ggplot(aes(x=count,y=z))+geom_boxplot()
Or in base R, just do:或者在基础 R 中,只需执行以下操作:
boxplot(t(scale(t(df[,-1]))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.