I am using an NER library ( entity ) to extract person names from sentences in a data frame.
If I run:
library(entity)
dat <- data.frame(texts=c('Henry went home', 'Drive a car', 'Two snowmen'), stringsAsFactors=FALSE)
person_entity(dat$texts)
I get a list of extracted names:
> person_entity(dat$texts)
[[1]]
[1] "Henry"
[[2]]
NULL
[[3]]
NULL
How can I append this list as an additional column to my data frame? The additional column could be a list of the extracted names, or even just the length of the list, eg:
dat <- data.frame(texts=c('Henry went home', 'Drive a car', 'Two snowmen'), person_count=c(1,0,0), stringsAsFactors=FALSE)
一种方法是使用lengths
来获取列表中各个元素的长度。
dat$person_count <- lengths(person_entity(dat$texts))
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.