试图用R找到唯一值

时间:2012-12-05 04:49:52

标签: r

我搜索了论坛,但找不到任何对我有用的东西。我在这里有一个独特的数据集,我记录了我每天每小时所做的事情,我只是想找到所有独特的条目。例如,有大约一百天左右的“睡眠”条目,“家庭作业”,“晚餐”,我只想找到我所做的所有可能的事情,所有独特的条目。

我尝试了独特的(数据),这对我来说无处可寻,我尝试过独特的(数据[,1])。如果我写一个'for'循环来做那个,我怎么能合并列表,并进一步削减他们的独特性?

> data[, 1]
 [1] morning prep  work          work          work          work         
 [6] work          work          work          work          homework     
[11] MNT           chat with Dan dinner        movie         movie        
[16] sleep         sleep         sleep         sleep         sleep        
[21] sleep         sleep         sleep         sleep                      
Levels:  chat with Dan dinner homework MNT morning prep movie sleep work

它的阶级是一个'因素'。独特可以很好地减少它,但是我如何将所有的日子组合成一个长的字符向量,我可以用来进行分析,例如......

sum(data=='sleep')

但是凭借我的列表,我将能够编写另一个'for'循环并轻松地总结一切......

1 个答案:

答案 0 :(得分:0)

我有一种感觉,你只是在寻找table功能。拉出“独特”因素并一次性计算频率:

myHomework <- data.frame(StuffIDid = c("morning prep", "work", "work", "work", 
                                       "work", "work", "work", "work", "work", 
                                       "homework", "MNT", "chat with Dan", 
                                       "dinner", "movie", "movie", "sleep", 
                                       "sleep", "sleep", "sleep", "sleep", 
                                       "sleep", "sleep", "sleep", "sleep"))
str(myHomework)
# 'data.frame':    24 obs. of  1 variable:
#  $ StuffIDid: Factor w/ 8 levels "chat with Dan",..: 5 8 8 8 8 8 8 8 8 3 ...
table(myHomework[, "StuffIDid"])
# 
# chat with Dan        dinner      homework           MNT  morning prep         movie 
#             1             1             1             1             1             2 
#         sleep          work 
#             9             8 

或者,作为data.frame

data.frame(table(myHomework[, "StuffIDid"]))
#            Var1 Freq
# 1 chat with Dan    1
# 2        dinner    1
# 3      homework    1
# 4           MNT    1
# 5  morning prep    1
# 6         movie    2
# 7         sleep    9
# 8          work    8