如何使用按ID分组的重复值生成R table(),以便在for循环中使用

如何使用按ID分组的重复值生成R table(),以便在for循环中使用,r,for-loop,duplicates,R,For Loop,Duplicates,我有重复的“类型”值,但它们与组ID代码关联 Type <- data.frame("Type" = c("A","A","A","B","B","B","B","C","C","C","C"), "ID"= c(1,1,1,2,2,2,2,3,3,4,4)) t <- table(Type[c("Type", "ID")]) a <- as.character() 但现在我被困在生成: [1] "One was associated

我有重复的“类型”值,但它们与组ID代码关联

Type <- data.frame("Type" = c("A","A","A","B","B","B","B","C","C","C","C"), 
                   "ID"= c(1,1,1,2,2,2,2,3,3,4,4))
t <- table(Type[c("Type", "ID")])

a <- as.character()
但现在我被困在生成:

[1] "One was associated with  (3, cases), one was associated with  (0 cases), one was associated with  (0 cases), one was associated with  (0 cases), one was associated with  (4 cases), one was associated with  (0 cases), one was associated with  (0 cases), one was associated with  (0 cases), one was associated with  (2 cases), one was associated with  (0 cases), one was associated with  (0 cases), and one was associated with  (2 cases)."
鉴于我希望我的最终产品显示如下:

[1] "One was associated with  A (3 cases), one was associated with B (4 cases), one was associated with C (2 cases), one was associated with C (2 cases).
我成功地处理了各种类型数据的for循环,但从未处理过任何分组数据

我选择了这个解决方案:

Library(Hmisc)
Library(stringi)

Type <- data.frame("Type" = c("A","A","A","B","B","B","B","C","C","C","C"), "ID"= c(1,1,1,2,2,2,2,3,3,4,4))
t <- table(Type)
a <- as.character()
for(i in 1:nlevels((as.factor(Type$ID)))){
  a <- paste0(a,"one was associated with"," ", sep = "")
  for(j in 1:nlevels(as.factor(Type$Type))){
    if(t[j,i] > 0){
      a <- paste0(a,levels(Type$Type)[j]," (",t[j,i]," cases),",sep = " ")
    }
  }
}

a <- capture.output(cat(a))
a <- substr(a, 1, nchar(a)-2)
a <- paste0(a, ".")
if(length(table(Type$ID)) <3){
  a <- sub(",", "", a)
}
a <- stri_replace_last_regex(a, "one", "and one",  opts_regex = list())
a <- capitalize(a)
a
库(Hmisc)
图书馆(stringi)

键入注释中提到的,然后使用dplyr:

library(dplyr)

sen <- Type %>% group_by(Type, ID) %>% summarise(n = n()) %>%
  mutate(sentence = paste0("one was associated with ",Type," (",n," cases)"))
paste(sen$sentence, collapse = ", ")
库(dplyr)
sen%group_by(Type,ID)%%>%summary(n=n())%%>%
变异(句子=paste0(“一个与“,”类型“,”n,“案例)”相关)
粘贴(sen$句子,折叠=“,”)

如评论中所述,但使用dplyr:

library(dplyr)

sen <- Type %>% group_by(Type, ID) %>% summarise(n = n()) %>%
  mutate(sentence = paste0("one was associated with ",Type," (",n," cases)"))
paste(sen$sentence, collapse = ", ")
库(dplyr)
sen%group_by(Type,ID)%%>%summary(n=n())%%>%
变异(句子=paste0(“一个与“,”类型“,”n,“案例)”相关)
粘贴(sen$句子,折叠=“,”)

我认为您应该迭代该表(请注意,您只需调用df的一个表,仅此而已),然后忽略空值:

Type <- data.frame("Type" = c("A","A","A","B","B","B","B","C","C","C","C"), "ID"= c(1,1,1,2,2,2,2,3,3,4,4))
t <- table(Type)
a <- as.character()
for(i in 1:nlevels((as.factor(Type$ID)))){
  a <- paste(a,"One was associated with ",sep = "")
  for(j in 1:nlevels(as.factor(Type$Type))){
    if(t[j,i] > 0){
      a <- paste(a,levels(Type$Type)[j],"(",t[j,i],"cases),",sep = " ")
    }
  }
}

我相信您将能够对不必要的空格和逗号进行更正。

我认为您应该迭代该表(请注意,您只需调用df的一个表,仅此而已),然后忽略空值:

Type <- data.frame("Type" = c("A","A","A","B","B","B","B","C","C","C","C"), "ID"= c(1,1,1,2,2,2,2,3,3,4,4))
t <- table(Type)
a <- as.character()
for(i in 1:nlevels((as.factor(Type$ID)))){
  a <- paste(a,"One was associated with ",sep = "")
  for(j in 1:nlevels(as.factor(Type$Type))){
    if(t[j,i] > 0){
      a <- paste(a,levels(Type$Type)[j],"(",t[j,i],"cases),",sep = " ")
    }
  }
}

我相信您将能够对不必要的空格和逗号进行更正。

如何
require(data.table);as.data.table(Type)[,sprintf(“一个与%s(%d个案例)关联)”,Type,.N),by=list(Type,ID)]$V1
?这(实际上是矩阵())是我用来将这些信息输入图表的,但我不知道如何处理它的语法和标点符号。
require(data.table)如何;as.data.table(Type)[,sprintf(“一个与%s(%d个案例)关联)”,Type,.N),by=list(Type,ID)]$V1
?这(实际上是矩阵())是我用来将此信息输入图表的,但我不知道如何对其进行语法和标点操作。对于所需的输出,应该将其更改为
groupby(Type,ID)
。构建句子的另一个选项是
sprintf(“一个与%s(%i)个案例相关联”,类型,n)
这应该更改为
groupby(类型,ID)
,以获得所需的输出。构建句子的另一个选项是
sprintf(“一个与%s(%i)个案例关联”),类型为n)
Type <- data.frame("Type" = c("A","A","A","B","B","B","B","C","C","C","C"), "ID"= c(1,1,1,2,2,2,2,3,3,4,4))
t <- table(Type)
a <- as.character()
for(i in 1:nlevels((as.factor(Type$ID)))){
  a <- paste(a,"One was associated with ",sep = "")
  for(j in 1:nlevels(as.factor(Type$Type))){
    if(t[j,i] > 0){
      a <- paste(a,levels(Type$Type)[j],"(",t[j,i],"cases),",sep = " ")
    }
  }
}
[1] "One was associated with  A ( 3 cases),One was associated with  B ( 4 cases),One was associated with  C ( 2 cases),One was associated with  C ( 2 cases),"