0

I'm having trouble extracting the first word from a list of words. I've tried substring, gsub, and str_extract but still haven't figured it out. Please advise. Thank you. Here is what I'm trying to do:

Word
"c("print", "printing", "prints")"
"c("take", "takes", "taking")"
"c("score", "scoring", "scored")"

I'm trying to extract the first word from the list that looks like this:

Extracted
print
take
score
cheklapkok
  • 409
  • 5
  • 9

2 Answers2

1

You can just use purrr::map with an index argument as follows:

If you want your output returned as a list:

  > purrr::map(Word, 1)
  # [[1]]
  # [1] "print"
  #
  # [[2]]
  # [1] "take"
  #
  # [[3]]
  # [1] "score"

If you want it returned as a vector:

  > purrr::map_chr(Word, 1)
  # [1] "print" "take"  "score"
Jared Wilber
  • 4,775
  • 29
  • 34
0

Using base R alone

##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])
akrun
  • 789,025
  • 32
  • 460
  • 575
Henry Cyranka
  • 2,783
  • 1
  • 14
  • 20