I have a vector of POS tags. And I want to create a global bigram vector of the POS tags. I have created the following function in R:
createbi<-function(a) {
for(i in 1:(length(a)-1)){
bigramlist<-c(bigramlist,(paste(a[i], a[i+1], sep=" ")))
}
}
here in ai want to pass a list of tags :
> a$POStags
[1] "DT" "VBZ" "DT" "JJ" "NN" "VBG" "IN" "DT" "NNS" "," "NNS" "," "CC" "NNS" "."
but I am not getting the bigrams in the bigramlist.I think the problem is with parameter passing. I want output as :
> bigramlist
[1] "DT VBZ" "VBZ DT" "DT JJ" "JJ NN" "NN VBG" "VBG IN" "IN DT" "DT NNS" "NNS ," ", NNS" "NNS ," ", CC" "CC NNS"
[14] "NNS .
can someone help me with this I am new to R
Here are three different approaches to your problem ordered from most to least preferable (in my opinion):
a) paste with head/tail
An alternative, perhaps more R-ish way to do it where x
denotes your vector a$POStags
, is to use paste
directly on two subsets of x
:
paste(head(x, -1), tail(x, -1))
#[1] "DT VBZ" "VBZ DT" "DT JJ" "JJ NN" "NN VBG" "VBG IN" "IN DT" "DT NNS" "NNS ," ", NNS" "NNS ,"
#[12] ", CC" "CC NNS" "NNS ."
Of course you can put that in a function too:
createbi <- function(a) paste(head(a, -1), tail(a, -1))
b) sapply
Alternatively you could use an sapply
loop as follows (but I would prefer the first approach):
createbi2 <- function(a) sapply(1:(length(a)-1), function(i) paste(a[i], a[i+1]))
c) for loop
If you insist on a for loop, I'd do it like this (notice preallocation of bigramlist
):
createbi <- function(a) {
n <- length(a)-1
bigramlist <- character(n)
for(i in 1:n){
bigramlist[i] <- paste(a[i], a[i+1])
}
bigramlist
}
But you see by now how much simpler approach a) is than writing the for loop.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.