我有多个因素来划分我的数据。
按一个因素(
uniqueGroup
),我想通过另一个因素将我的数据子集(
distance
),我想首先通过“移动阈值”对数据进行分类,然后测试各组之间的统计差异。
我创建了一个函数
movThreshold
对我的数据进行分类,并通过
wilcox.test
。要改变不同的阈值,我只需运行
lapply(th.list, # list of thresholds
movThreshold, # my function
tab = tab, # original data
dependent = "infGrad") # dependent variable
现在我意识到事实上
我需要首先将我的数据子集
通过
唯一组
和
然后改变阈值
.但我不知道如何在我的
lapply
密码
我的虚拟数据:
set.seed(10)
infGrad <- c(rnorm(20, mean=14, sd=8),
rnorm(20, mean=13, sd=5),
rnorm(20, mean=8, sd=2),
rnorm(20, mean=7, sd=1))
distance <- rep(c(1:4), each = 20)
uniqueGroup <- rep(c("x", "y"), 40)
tab<-data.frame(infGrad, distance, uniqueGroup)
# Create moving threshold function &
# test for original data
# ============================================
movThreshold <- function(th, tab, dependent, ...) {
# Classify data
tab$group<- ifelse(tab$distance < th, "a", "b")
# Calculate wincoxon test - as I have only two groups
test<-wilcox.test(tab[[dependent]] ~ as.factor(group), # specify column name
data = tab)
# Put results in a vector
c(th, unique(tab$uniqueGroup), dependent, uniqueGroup, round(test$p.value, 3))
}
# Define two vectors to run through
# unique group
gr.list<-unique(tab$uniqueGroup)
# unique threshold
th.list<-c(2,3,4)
如何运行
拉普拉
超过两个列表??
lapply(c(th.list,gr.list), # iterate over two vectors, DOES not work!!
movThreshold,
tab = tab,
dependent = "infGrad")
在我之前的问题中(
Kruskal-Wallis test: create lapply function to subset data.frame?
),我学习了如何遍历表中的各个子集:
lapply(split(tab, df$uniqueGroup), movThreshold})
但是,如何一次遍历子集和阈值呢?