计算r中字符串中唯一元素的最佳方法

时间:2015-12-08 00:12:02

标签: r unique

我还是R的初学者,我有一个问题!

我有222.000个观察数据框,我很有意思的是一个名为id的特定列。问题是它可以在同一个字符串中用','分隔,并且我想计算每个字符串中的唯一元素(我的意思是在第一个数据帧的每个字符串中)。 例如:

      id                       results

0000001,0000003                   2

0000002,0000002                   1

0010001,0001006,0010001           2

我已经使用函数'str_split_fixed'来分隔同一个字符串中的所有id,并将结果放在一个新的数据框中(因此我知道我只有1个字符串的字符串或字符串中没有任何内容)。问题是可能多达68',所以新的数据框架很大,有68个柱和220,000个观测值,需要很长时间(可能有15个秒)。使用了apply函数后知道所有的独特之处。

有人知道更有效的方式还是有想法?

最后,我使用了以下代码:

sapply(id, function(x) 
           length(    # count items
             unique(   # that are unique
                scan(   # when arguments are presented to scan as text 
                      text=x, what="", sep =",",  # when separated by ","
                      quiet=TRUE)))  )

但是有一条消息错误:

Error in textConnection(text, encoding = "UTF-8") : 
  argument 'text' incorrect 
6 textConnection(text, encoding = "UTF-8") 
5 scan(text = x, what = "", sep = ",", quiet = TRUE) 
4 unique(scan(text = x, what = "", sep = ",", quiet = TRUE)) 
3 FUN(X[[i]], ...) 
2 lapply(X = X, FUN = FUN, ...) 
1 sapply(id, function(x) length(unique(scan(text = x, 
    what = "", sep = ",", quiet = TRUE)))) 

我的R版本是:

 R version 3.2.2 (2015-08-14)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)

locale:
[1] fr_FR.UTF-8/fr_FR.UTF-8/fr_FR.UTF-8/C/fr_FR.UTF-8/fr_FR.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stringr_1.0.0 plyr_1.8.3   

loaded via a namespace (and not attached):
[1] magrittr_1.5  tools_3.2.2   Rcpp_0.12.2   stringi_1.0-1
> 

我试过这个:Encoding(id) <- "UTF-8" 但结果是:

Error in `Encoding<-`(`*tmp*`, value = "UTF-8")  

,dput(id)的输出来自:

   [9987,] "2320212,2320230"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
  [9988,] "4530090,4530917"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
  [9989,] "8532412"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  [9990,] "4560292"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  [9991,] "4540375"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  [9992,] "3311324"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  [9993,] "4540030"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  [9994,] "9010000"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  [9995,] "2811810"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  [9996,] "3311000"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  [9997,] "4540030"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  [9998,] "4540215"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  [9999,] "1541201"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
 [10000,] "2423810"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
 [ getOption("max.print") est atteint -- 90000 lignes omises ]

输出很大,所以我只发布结尾和第一行:

 [9002,] "9460000"   

dput( head(data$id) )

"9460000,9433000", "9460000,9436000", "9460000,9437000", 
"9510000", "9510010", "9510030", "9510090", "9910000", "9910020", 
"9910040", "9910090", "D", "FIELD_NOT_FOUND", "I"), class = "factor")  

先谢谢你,杰夫

1 个答案:

答案 0 :(得分:1)

sapply(id, function(x) 
           length(    # count items
             unique(   # that are unique
                scan(   # when arguments are presented to scan as text 
                      text=x, what="", sep =",",  # when separated by ","
                      quiet=TRUE)))  )
# --- result: first typed line is 'names' of the items, not the results.
    1 2,3,4   1,1 
    1     3     1 

参数text=x应允许scan接受长度为1的字符元素,并将其分解为分隔符参数值的分区中的组件。这些将从id向量逐个元素传递给匿名函数(如果它来自数据帧,则逐行传递)。