我有一个类似于以下内容的分类数据集:
A<-data.frame(animal=c("cat","cat","cat","dog","dog","dog","elephant","elephant","elephant"),
color =c(rep(c("blue","red","green"),3)))
Run Code Online (Sandbox Code Playgroud)
我想订购它,以便动物用狗,然后大象,然后猫,然后颜色分为绿色,蓝色,然后红色.所以最后它看起来像
狗绿色,狗蓝色,狗红色,大象绿色,大象蓝色,...
我正在创建一个循环的麻烦.我的意图是循环将看到值下降的位置并给该值另一个名称.这是我想要做的一个例子:
a<-rnorm(10,0,1)
b<-rnorm(10,0,1)
testing<-data.frame(a,b)
testing2<-testing
for (i in 1:nrow(testing2)){
for (j in 1:ncol(testing2)){
if (testing2[i,j]>1) testing2[i,j]<-"More"
else if (testing2[i,j]<(-1)) testing2[i,j]<-"Less"
else testing2[i,j]<-"Same"
}
}
Run Code Online (Sandbox Code Playgroud)
当我查看testing2并将其与测试进行比较时,它与它应该做的事情不匹配."更多"似乎有效,但它在"少"和"相同"之间混淆.