문자열 열의 각 행에서 주어진 문자의 발생 횟수를 계산하는 방법은 무엇입니까?
특정 변수에 텍스트 문자열이 포함 된 data.frame이 있습니다. 각 개별 문자열에서 주어진 문자의 발생 횟수를 계산하고 싶습니다.
예:
q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"))
문자열에서 "a"의 발생 횟수 (예 : c (2,1,0))를 사용하여 q.data에 대한 새 열을 만들고 싶습니다.
내가 관리 한 유일한 복잡한 접근 방식은 다음과 같습니다.
string.counter<-function(strings, pattern){
counts<-NULL
for(i in 1:length(strings)){
counts[i]<-length(attr(gregexpr(pattern,strings[i])[[1]], "match.length")[attr(gregexpr(pattern,strings[i])[[1]], "match.length")>0])
}
return(counts)
}
string.counter(strings=q.data$string, pattern="a")
number string number.of.a
1 1 greatgreat 2
2 2 magic 1
3 3 not 0
stringr 패키지는 str_count
관심있는 작업을 수행하는 것처럼 보이는 기능을 제공합니다 .
# Load your example data
q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = F)
library(stringr)
# Count the number of 'a's in each element of string
q.data$number.of.a <- str_count(q.data$string, "a")
q.data
# number string number.of.a
#1 1 greatgreat 2
#2 2 magic 1
#3 3 not 0
기본 R을 떠나고 싶지 않다면 여기에 상당히 간결하고 표현 가능한 가능성이 있습니다.
x <- q.data$string
lengths(regmatches(x, gregexpr("a", x)))
# [1] 2 1 0
nchar(as.character(q.data$string)) -nchar( gsub("a", "", q.data$string))
[1] 2 1 0
nchar에 전달하기 전에 factor 변수를 문자로 강제 변환합니다. 정규식 함수는 내부적으로이를 수행하는 것으로 보입니다.
다음은 벤치 마크 결과입니다 (테스트 크기를 3000 행으로 확장).
q.data<-q.data[rep(1:NROW(q.data), 1000),]
str(q.data)
'data.frame': 3000 obs. of 3 variables:
$ number : int 1 2 3 1 2 3 1 2 3 1 ...
$ string : Factor w/ 3 levels "greatgreat","magic",..: 1 2 3 1 2 3 1 2 3 1 ...
$ number.of.a: int 2 1 0 2 1 0 2 1 0 2 ...
benchmark( Dason = { q.data$number.of.a <- str_count(as.character(q.data$string), "a") },
Tim = {resT <- sapply(as.character(q.data$string), function(x, letter = "a"){
sum(unlist(strsplit(x, split = "")) == letter) }) },
DWin = {resW <- nchar(as.character(q.data$string)) -nchar( gsub("a", "", q.data$string))},
Josh = {x <- sapply(regmatches(q.data$string, gregexpr("g",q.data$string )), length)}, replications=100)
#-----------------------
test replications elapsed relative user.self sys.self user.child sys.child
1 Dason 100 4.173 9.959427 2.985 1.204 0 0
3 DWin 100 0.419 1.000000 0.417 0.003 0 0
4 Josh 100 18.635 44.474940 17.883 0.827 0 0
2 Tim 100 3.705 8.842482 3.646 0.072 0 0
sum(charToRaw("abc.d.aa") == charToRaw('.'))
좋은 선택입니다.
I'm sure someone can do better, but this works:
sapply(as.character(q.data$string), function(x, letter = "a"){
sum(unlist(strsplit(x, split = "")) == letter)
})
greatgreat magic not
2 1 0
or in a function:
countLetter <- function(charvec, letter){
sapply(charvec, function(x, letter){
sum(unlist(strsplit(x, split = "")) == letter)
}, letter = letter)
}
countLetter(as.character(q.data$string),"a")
The stringi
package provides the functions stri_count
and stri_count_fixed
which are very fast.
stringi::stri_count(q.data$string, fixed = "a")
# [1] 2 1 0
benchmark
Compared to the fastest approach from @42-'s answer and to the equivalent function from the stringr
package for a vector with 30.000 elements.
library(microbenchmark)
benchmark <- microbenchmark(
stringi = stringi::stri_count(test.data$string, fixed = "a"),
baseR = nchar(test.data$string) - nchar(gsub("a", "", test.data$string, fixed = TRUE)),
stringr = str_count(test.data$string, "a")
)
autoplot(benchmark)
data
q.data <- data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = FALSE)
test.data <- q.data[rep(1:NROW(q.data), 10000),]
You could just use string division
require(roperators)
my_strings <- c('apple', banana', 'pear', 'melon')
my_strings %s/% 'a'
Which will give you 1, 3, 1, 0. You can also use string division with regular expressions and whole words.
The easiest and the cleanest way IMHO is :
q.data$number.of.a <- lengths(gregexpr('a', q.data$string))
# number string number.of.a`
#1 1 greatgreat 2`
#2 2 magic 1`
#3 3 not 0`
The question below has been moved here, but it seems this page doesn't directly answer to Farah El's question. How to find number 1s in 101 in R
So, I'll write an answer here, just in case.
library(magrittr)
n %>% # n is a number you'd like to inspect
as.character() %>%
str_count(pattern = "1")
https://stackoverflow.com/users/8931457/farah-el
A variation of https://stackoverflow.com/a/12430764/589165 is
> nchar(gsub("[^a]", "", q.data$string))
[1] 2 1 0
s <- "aababacababaaathhhhhslsls jsjsjjsaa ghhaalll"
p <- "a"
s2 <- gsub(p,"",s)
numOcc <- nchar(s) - nchar(s2)
May not be the efficient one but solve my purpose.
'program tip' 카테고리의 다른 글
"APR 기반 Apache Tomcat 네이티브 라이브러리를 찾을 수 없음"은 무엇을 의미합니까? (0) | 2020.09.05 |
---|---|
Windows 명령 프롬프트를 통해 암호 인증으로 ssh 실행 (0) | 2020.09.05 |
Travis-CI를 C # 또는 F #과 함께 사용하는 방법 (0) | 2020.09.05 |
두 개의 MySQL 테이블을 병합하려면 어떻게해야합니까? (0) | 2020.09.05 |
git push local branch with same name as remote tag (0) | 2020.09.05 |