簡體   English   中英

將列分組為R數據幀中的計數

[英]Grouping columns into count in R dataframe

所以我在一個數據幀中總共有這四個不同的列

     port            ip                service      numberOfTimes
1     22         11.11.79.100            ssh           16
2     80         11.11.79.100            www           19
3     111        11.13.79.110            ipw           21
4     123        11.13.79.110            ssh           50
5     22         64.50.80.140            cde           45
6     80         64.50.80.140            www           16
7     22         71.11.64.100            ssh           234
8     80         71.11.64.100            you           33
9     22         100.15.31.1             ssh           99
10    41         120.15.31.12            has           19

所以我有以下問題:

是否可以將r分組為以下內容,使其可以變成類似的東西?

port       ip(count of same ip)   service     numberOfTimes
 22             4                  ssh         399 (#1+#5+#7+#9)
 80             3                  www         68 (#2+#6+#8)

其余的端口等等

使用dplyr ,這非常簡單:

testData %>%
  group_by(port, service) %>%
  summarise(`Number of IPs` = n_distinct(ip)
            , `Total number of times` = sum(numberOfTimes))

您所包括的樣本數據的哪個給出:

   port service `Number of IPs` `Total number of times`
  <int>   <chr>           <int>                   <int>
1    22     cde               1                      45
2    22     ssh               3                     349
3    41     has               1                      19
4    80     www               2                      35
5    80     you               1                      33
6   111     ipw               1                      21
7   123     ssh               1                      50

如果您遇到某種錯誤(在注釋中可以忽略),則需要提供確實導致該錯誤的數據,然后人們才能幫助您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM