简体   繁体   English

编写公式并尝试在 R 中循环

[英]Writing a formula and trying to loop it in R

   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.我有一个看起来像这样的数据头,数据集称为 data_LOG。 I want to z-score all these columns.我想对所有这些列进行 z 评分。 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.因为有超过 1000 列,我想循环公式,以便我可以快速将所有这些值更改为 z 分数。 The equation for z-score is (y-mean(y)/sd(y)). z 分数的等式是 (y-mean(y)/sd(y))。 So i made a function called 'zscore'.所以我做了一个名为'zscore'的function。

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.我希望 z 分数数据位于我称为 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()您可以将apply()standardize()scale()结合使用

dataZ <- apply(data_LOG, 2, scale) # margins = 2, indicates that the function is applied columns

HTH:) HTH:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM