所以我有50个变量的值范围从1到4,我想要统计有多少个是1或2以及有多少个是3或4.在for循环中编写一个带有粘贴功能的ifelse语句R
即ABC1 = 2,ABC2 = 2,... abc50 = 3
和下面是我的代码
#Create new variable to store the counted number to
abc.low=0
abc.high=0
这里是我停留在代码(这是行不通的)
for (i in 1:50){
ifelse (paste("abc",i,sep="")==1|paste("abc",i,sep="")==2,
(abc.low<-abc.low<-1),(abc.low<-abc.low))
}
for (i in 1:50){
ifelse (paste("abc",i,sep="")==3|paste("abc",i,sep="")==4,
(abc.high<-abc.high<-1),(abc.high<-abc.high))
}
我假设粘贴功能不适合我所要做的。
即)
abc1=3
abc1==3
#True
paste("abc",1,sep="")==3
# False
其中粘贴功能应该返回真,我的目的。
我感谢您的意见!
通常,最好将这些变量存储在列表中而不是单个对象。 – Justin
这是可能的,但你可以将你的变量存储在一个'list'或一个向量中吗?你目前的方法看起来不像“R”那样。 – ialm