本文介绍了for循环&如果功能在R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 ID类别 1 a 1 b 1 c 2 a 2 b 3 a 3 b 4 a 5 a 我想使用if函数的for循环来添加另一列来计算每个分组ID,就像以下数列: ID类别计数 1 a 1 1 b 2 1 c 3 2 a 1 2 b 2 3 a 1 3 b 2 4 a 1 5 a 1 我的代码是(输出是表名): 输出1 [ i,rn]< - output1 [i-1,rn] + 1 } else { output1 [i,rn]< -1 } } $ $ $ $ $ $ $ $> ID类别Co unt 1 a 1 1 b 1 1 c 1 2 a 1 2 b 1 3 a 1 3 b 1 4 a 1 5 a 1 请帮我...谢谢 解决方案有些软件包和矢量化的方法可以完成这个任务,但是如果你在循环练习, $ b output1 $ rn for(i in 2:nrow(output1)){ if(output1 [i,1] == output1 [i-1,1]){ output1 [i,rn] } else { output1 [i,rn] } } 使用原始代码,当您调用 output1 [i-1,rn] + 1 时你的循环的第三行,你引用了第一遍中不存在的行。通过首先创建行并使用值 1 填充它,可以给循环显式引用一些内容。 output1 #ID类别#1 1 a 1 #2 1 b 2 #3 1 c 3 #4 2 a 1 #5 2 b 2 #6 3 a 1 #7 3 b 2 #8 4 a 1 #9 5 a 1 使用包dplyr,您可以快速完成: library(dplyr) output1%>%group_by(ID)%>%mutate(rn = 1:n()) 或用data.table: setDT(output1)[,rn:= 1:.N,by = ID] 使用 base R ,您也可以使用: output1 $ rn 这两个软件包中有插图和教程并在R控制台中搜索?ave 以获得最后的方法。 I was writing a loop with if function in R. The table is like below:ID category1 a1 b1 c2 a2 b3 a3 b4 a5 aI want to use the for loop with if function to add another column to count each grouped ID, like below count column:ID category Count1 a 11 b 21 c 32 a 12 b 23 a 13 b 24 a 15 a 1My code is (output is the table name):for (i in 2:nrow(output1)){ if(output1[i,1] == output[i-1,1]){ output1[i,"rn"]<- output1[i-1,"rn"]+1 } else{ output1[i,"rn"]<-1 }}But the result returns as all count column values are all "1".ID category Count1 a 11 b 11 c 12 a 12 b 13 a 13 b 14 a 15 a 1Please help me out... Thanks 解决方案 There are packages and vectorized ways to do this task, but if you are practicing with loops try:output1$rn <- 1for (i in 2:nrow(output1)){ if(output1[i,1] == output1[i-1,1]){ output1[i,"rn"]<- output1[i-1,"rn"]+1 } else{ output1[i,"rn"]<-1 }}With your original code, when you made this call output1[i-1,"rn"]+1 in the third line of your loop, you were referencing a row that didn't exist on the first pass. By first creating the row and filling it with the value 1, you give the loop something explicit to refer to.output1# ID category rn# 1 1 a 1# 2 1 b 2# 3 1 c 3# 4 2 a 1# 5 2 b 2# 6 3 a 1# 7 3 b 2# 8 4 a 1# 9 5 a 1With the package dplyr you can accomplish it quickly with:library(dplyr)output1 %>% group_by(ID) %>% mutate(rn = 1:n())Or with data.table:library(data.table)setDT(output1)[,rn := 1:.N, by=ID]With base R you can also use:output1$rn <- with(output1, ave(as.character(category), ID, FUN=seq))There are vignettes and tutorials on the two packages mentioned, and by searching ?ave in the R console for the last approach. 这篇关于for循环&如果功能在R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-12 13:44