简体   繁体   English

R:应用dprime函数-apply()错误'dim(X)必须具有正长度”

[英]R: Applying a dprime function - apply() error 'dim(X) must have a positive length'

I'm trying to write a dprime function where I subtract FA from hits z-scores to get a dprime value for each participant. 我正在尝试编写dprime函数,在该函数中,我从命中z分数中减去FA,以获取每个参与者的dprime值。 Participants are rows and z-scores are columns. 参与者是行,z分数是列。 My dataset (newData) is 18x16 but I want to ignore the first 8 columns. 我的数据集(newData)为18x16,但我想忽略前8列。 I want to repeat the dprime calculation for each of condition and create a new 18x4 dataset (newData2) with the output. 我想为每个条件重复dprime计算,并使用输出创建一个新的18x4数据集(newData2)。

# Function to apply
dprimeFun <- function(twoColumns){
  # Give the function two columns at a time
  zH <- twoColumns[ 1 ]
  zF <- twoColumns[ 2 ]
  dprime <- zH - zF 
  return(dprime)
}

# Apply the function
newData2 <- cbind(
  # Condition 1 dprimes
  apply(
    newData[ c( "zH1","zF1" ) ], MARGIN=1, FUN=dprimeFun
  ),
  # Repeat for each condition
)

When I run this I get the following error: 运行此命令时,出现以下错误:

Error in apply(newData[c("zH1", "zF1")], MARGIN = 1, FUN = dprimeFun) : 
  dim(X) must have a positive length

I found this question where somebody had the same error message, however I think their solution (just apply the function directly to the data subset) won't work for me because I'd have to do so 72 times. 我在有人有相同错误消息的地方发现了这个问题 ,但是我认为他们的解决方案(仅将函数直接应用于数据子集)对我不起作用,因为我必须这样做72次。

I also read the R FAQ 'Why do matrices lose dimensions?' 我还阅读了R常见问题解答“为什么矩阵会丢失维?” but I don't understand how it applies to my 18x16 dataset. 但我不明白它如何适用于我的18x16数据集。

I'm an R newb so help would be much appreciated. 我是R newb,非常感谢您的帮助。

Edit: 编辑:

# newData structure
> str(newData)
 num [1:18, 1:16] 0.833 0.833 0.833 0.833 0.833 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:16] "H1" "F1" "H2" "F2" ...

# newData column names
> colnames(newData)
 [1] "H1"  "F1"  "H2"  "F2"  "H3"  "F3"  "H4"  "F4"  "zH1" "zF1" "zH2" "zF2" "zH3" "zF3"
[15] "zH4" "zF4"

Perhaps you want... 也许你想要...

newData$dprime <- dprimeFun(newData[,c("zH1", "zF1")])

But it's extraordinarily hard to tell. 但这很难说。

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

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