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
  • 439
  • 1
  • 5
  • 11

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
  • 6,038
  • 1
  • 32
  • 35
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
  • 874,273
  • 37
  • 540
  • 662
Henry Cyranka
  • 2,970
  • 1
  • 16
  • 21