将带有 NULL 的列表附加到数据框

Append list with NULLs to data frame

我正在使用 NER 库 (entity) 从数据框中的句子中提取人名。

如果我运行:

library(entity)
dat <- data.frame(texts=c('Henry went home', 'Drive a car', 'Two snowmen'), stringsAsFactors=FALSE)
person_entity(dat$texts)

我得到了提取名称的列表:

> person_entity(dat$texts)
[[1]]
[1] "Henry"

[[2]]
NULL

[[3]]
NULL

如何将此列表作为附加列附加到我的数据框中?附加列可以是提取名称的列表,或者甚至只是列表的长度,例如:

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))