r-根据列表分配组

时间:2018-08-24 15:52:22

标签: r match

我指的是这个主题:

Scatter plot with groups

有没有一种方法可以根据矢量创建组

类似

n <- c("N1",    "N2",   "N3",   "N4",   "N5",   "N6",   "N7") 
t <- c("T1",    "T2", "T3", "T4",   "T5",   "T6",   "T7",   "T8",   "T9",   "T10",  "T11",  "T12",  "T13",  "T14") 
m <- c("M1",    "M2",   "M3",   "M4", "M5", "M6",   "M7",   "M8",   "M9",   "M10")

并且要实现此代码中的组决策-而不是使用startwith?

count = 0
for(value in newData$variable){

        # To get the group of each value

        if(startsWith(value, 'N')){

                group <- 'N'

        }else if(startsWith(value, 'T')){

                group <- 'T'

        }else if(startsWith(value, 'M')){

                group <- 'M'
        }

        # Storing the group name into a vector
        if(count == 0){

                vectorGroup <- group
        }else{

                vectorGroup <- c(vectorGroup, group)

        }

        count <- count + 1

}

newData$group <- vectorGroup

我有点困惑。我尝试使用“ ==”或“ match”进行分配,但尚未解决。

非常感谢。  丹尼斯

0 个答案:

没有答案