-1

我搜索了论坛,找不到任何对我有用的东西。我在这里有一个独特的数据集,我记录了我一天中每个小时所做的事情,我现在只想找到所有独特的条目。例如,有 'sleep' 条目,'homework','dinner',所有这些都跨越了一百多天,我只想找到我当时做过的所有可能的事情,所有唯一的条目。

我已经尝试过 unique(data),它没有让我到任何地方,并且我尝试过 unique(data[, 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

它的等级是一个“因素”。Unique 很好地减少了它,但是我如何将所有的日子组合成一个长字符向量,我可以用它来做我的分析,例如......

sum(data=='sleep')

但是通过我的列表,我将能够编写另一个“for”循环并轻松总结所有内容......

4

1 回答 1

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
于 2012-12-05T07:07:32.170 回答