A2.DM19C.MICSw… A2.DM19C.MICSw… A2.IF12C.MICSwm… A2.DM12C.MICSwm… A2.HA12C.MICSwm…
<dbl> <dbl> <dbl> <dbl> <dbl>
1 -0.131 0.0516 -0.294 1.29 0.144
2 -0.175 -0.0250 -0.183 1.31 0.146
3 -0.128 0.0691 -0.294 1.31 0.0224
4 -0.175 0.0359 -0.294 1.31 0.136
5 -0.142 0.0169 -0.295 1.31 0.0239
6 -0.252 -0.0918 -0.272 1.33 -0.0263
I have a head of data that looks like this and the dataset is called data_LOG. I want to z-score all these columns. Because there are over 1000 columns, I want to loop the formula so that I can quickly change all these values to a z-score. The equation for z-score is (y-mean(y)/sd(y)). So i made a function called 'zscore'.
zscore <- function(r){
Cal <- (r-mean(r))/sd(r)
return(Cal)
}
Which works just fine when tested against the first column. I want the z-score data to be in a new data frame i call dataZ.
dataZ <- data_log
However, when i attempt to loop the formula, i get an error code.
for (i in 1:ncol(data_log)) {
dataZ[,i] <- zscore(data_log[,i])
}
Error in is.data.frame(x) :
'list' object cannot be coerced to type 'double'
In addition: Warning message:
In mean.default(r) :
Show Traceback
Rerun with Debug
Error in is.data.frame(x) :
'list' object cannot be coerced to type 'double'
I am unsure what this means and how to fix it? please help!
If you want to keep your approach try this
dataZ <- NULL
for (i in 1:ncol(df)) {
z <- zscore(df[[i]])
dataZ <- cbind(z, dataZ)
}
dataZ <- as.data.frame(dataZ)
You could use apply()
in combination with standardize()
or scale()
dataZ <- apply(data_LOG, 2, scale) # margins = 2, indicates that the function is applied columns
HTH:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.