gpt4 book ai didi

r - 关于如何使用 for 循环比较列和输入向量的建议

转载 作者:行者123 更新时间:2023-12-04 08:35:16 24 4
gpt4 key购买 nike

我想制作一个计数向量,将多列与名为 truth 的列进行比较。 .我真的不确定这是如何工作的,所以任何建议都会有所帮助,因为到目前为止这对我不起作用。

k=13
for(i in 1:k){
TPR<- tumor %>% filter(tumor1[,i]==T) %>% filter(tumor1[,i]==truth) %>% tally()
}
为了重现性,您可以使用此代码,但它真的很尴尬,所以是这样的:
library(ggplot2)
library(dplyr)
library(tidyverse)

malig<-c(.49, .36, .58, .56, .61, .66)
benign<-c(.42, .22, .26, .53, .31 ,.41)

tumor<- NULL
tumor$malig<- malig
tumor$benign<- benign
tumor<-tumor %>% as.data.frame()

tumor<- tumor %>% pivot_longer(cols=c(malig,benign), names_to="status") %>% arrange(status)

cutoffs<-c(.2, .25, .3, .35, .37, .4, .45, .5, .55, .57, .6, .65, .7)

tumor<- tumor %>% mutate(tumor, Twenty = ifelse(value > .2, T, F))
tumor<- tumor %>% mutate(tumor, Twentyfive = ifelse(value > .25, T, F))
tumor<- tumor %>% mutate(tumor, Thirty = ifelse(value > .3, T, F))
tumor<- tumor %>% mutate(tumor, Thirtyfive = ifelse(value > .35, T, F))
tumor<- tumor %>% mutate(tumor, Thirtyseven = ifelse(value > .37, T, F))
tumor<- tumor %>% mutate(tumor, Forty = ifelse(value > .4, T, F))
tumor<- tumor %>% mutate(tumor, Fortyfive = ifelse(value > .45, T, F))
tumor<- tumor %>% mutate(tumor, Fifty = ifelse(value > .5, T, F))
tumor<- tumor %>% mutate(tumor, Fiftyfive = ifelse(value > .55, T, F))
tumor<- tumor %>% mutate(tumor, Fiftyseven = ifelse(value > .57, T, F))
tumor<- tumor %>% mutate(tumor, Sixty = ifelse(value > .6, T, F))
tumor<- tumor %>% mutate(tumor, Sixtyfive = ifelse(value > .65, T, F))
tumor<- tumor %>% mutate(tumor, Seventy = ifelse(value > .7, T, F))

tumor1<- tumor %>% mutate(tumor, truth=ifelse(status=="malig", T, F))
tumor1<- tumor1 %>% select(-value,-status)
k=13
for(i in 1:k){
TPR<- tumor1 %>% filter(tumor1[,i]==T) %>% filter(tumor1[,i]==truth) %>% tally()

}

最佳答案

也许你正在寻找这样的东西:

library(dplyr)

tumor1 %>% summarise(across(Twenty:Seventy, ~sum(. & truth)))
在基数 R 中将是:
colSums(tumor1[-ncol(tumor1)] & tumor1$truth)

关于r - 关于如何使用 for 循环比较列和输入向量的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64829575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com