我在从单词列表中提取第一个单词时遇到麻烦。我试过子字符串,gsub和str_extract,但仍然没有弄清楚。请指教。谢谢。这是我想要做的:
Word
"c("print", "printing", "prints")"
"c("take", "takes", "taking")"
"c("score", "scoring", "scored")"
我正在尝试从列表中提取第一个单词,如下所示:
Extracted
print
take
score
答案 0 :(得分:1)
您可以仅将purrr::map
与索引参数一起使用,如下所示:
如果您希望将输出作为列表返回:
> purrr::map(Word, 1)
# [[1]]
# [1] "print"
#
# [[2]]
# [1] "take"
#
# [[3]]
# [1] "score"
如果您希望它作为向量返回:
> purrr::map_chr(Word, 1)
# [1] "print" "take" "score"
答案 1 :(得分:0)
仅使用基数R
##Just to recreate the data
df <- tibble(
Word= list(c("print", "printing", "prints"),c("take", "takes", "taking"),c("score", "scoring", "scored")))
###
df$Extracted <- sapply(1:length(df$Word), function(i)df$Word[[i]][1])