簡體   English   中英

如何計算 R 數據框中字符串中“c(\”) 的出現次數?

[英]How to count the occurrences of "c(\" in a string in a data frame in R?

我有一個數據框,其中某些列包含來自 Mplus 的錯誤和警告消息。 文本以一種奇怪的格式保存,因此我希望通過計算單元格中 c(\ 的出現次數來簡單地計算消息的數量,而不是嘗試處理每條消息,因為它是出現在每個消息之前的唯一字符組合警告或錯誤。

例如,一個單元格包含以下消息:

[[1]]
[1] "c(\"All variables are uncorrelated with all other variables within class.\""
[2] " \"Check that this is what is intended.\""                                  
[3] " \"1 WARNING(S) FOUND IN THE INPUT INSTRUCTIONS\")"                         
[4] " c(\"WARNING:  THE BEST LOGLIKELIHOOD VALUE WAS NOT REPLICATED.  THE\""     
[5] " \"SOLUTION MAY NOT BE TRUSTWORTHY DUE TO LOCAL MAXIMA.  INCREASE THE\""    
[6] " \"NUMBER OF RANDOM STARTS.\")" 

而另一個包含一個較短的消息,如下所示:

[[1]]
[1] "c(\"All variables are uncorrelated with all other variables within class.\""
[2] " \"Check that this is what is intended.\""                                  
[3] " \"1 WARNING(S) FOUND IN THE INPUT INSTRUCTIONS\")" 

我嘗試使用 str_count 幾種不同的方式,包括我最近的嘗試:

    str_count(test#, '//c(\//')

但我收到錯誤: Error: '\/' is an unrecognized escape in character string starting "'//c(\/" 。理想情況下,第一個示例返回 2,第二個示例返回 1。

當這個唯一字符串包含大多數封裝它或 escaping 的字符時,我如何計算它的出現次數?

這里有一些易於使用的測試代碼來試一試!

test1 <- '"c(\"All variables are uncorrelated with all other variables within class.\"" " \"Check that this is what is intended.\"" " \"1 WARNING(S) FOUND IN THE INPUT INSTRUCTIONS\")"'

test2 <- '"c(\"All variables are uncorrelated with all other variables within class.\"" " \"Check that this is what is intended.\"" " \"1 WARNING(S) FOUND IN THE INPUT INSTRUCTIONS\")"'

您可以嘗試在我的評論中減少要計算的部分

str_count(test1, "c\\(")

或者您可以通過檢查c(\"來延長參數並使用 fixed() 參數:

str_count(test1, fixed('c(\"'))

你可以試試gregexpr()

test1 <- '"c(\" foo bar baz'
test2 <- '"c(\" foo bar baz "c(\" baz bar foo'

length(unlist(gregexpr('c\\(', test1)))
# [1] 1
length(unlist(gregexpr('c\\(', test2)))
# [1] 2
length(unlist(gregexpr('c\\(', list(test1, test2))))
# [1] 3

暫無
暫無

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

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