r语言if语句后面必须要大括号吗

Python013

r语言if语句后面必须要大括号吗,第1张

if 语句后有大括号, 则大括号里面所有的语句都会在if表达式为真时执行;如果没大括号,则if表达式为真时只会执行第一条语句. 例如: #include int main(){ if (true) { printf("11111111111\n")// 这两条都会打印 printf("11111111111\n")}

# 看了一下楼主的编码 ,我的理解是把美国的hotdog 标记出来,修改如下:

    for(i in 1:length(hotdogs$Country)){

        if(hotdogs$Country[i]=="United States"){

            hotdogs$Country[i]<- c("#821122")}

        else{hotdogs$Country[i] <- c("#cccccc")}

    }

如果数据量比较大的话这样比较耗时间,建议用:

Country<- hotdogs$Country

index<- Country=="United States" 

Country[index]<- "#821122"

index1<-Country!="United States" 

Country[index1]<- "#cccccc"

print(Country)

虽然比较简单,但是速度较快。。。