cheklapkok
cheklapkok

Reputation: 439

How to extract second value from a list of lists and give NA to lists with no second value?

I'd like to extract the first and second values from a list of lists. I was able to extract the first value with no issue. However, it gives me an error when I was trying to extract the second value because not all lists from the suggestion column has more than one value. How can I extract the second value from the suggestion column in mydf_1 and generate NA to those with no second value?

Below are the codes I wrote to get to the first suggestion, but when I do

mydf_1$second_suggestion <- lapply(mydf_1$suggestion, `[[`, 2)

it gives this error: Error in FUN(X[[i]], ...) : subscript out of bounds

Thanks.

# create a data frame contains words
mydf <- data.frame("words"=c("banna", "pocorn and drnk", "trael", "rabbitt", 
                               "emptey", "ebay", "templete", "interne", "bing", 
                               "methog", "tullius"), stringsAsFactors=FALSE)

# add a custom word to the dictionary$
library(hunspell)
mydict_hunspell <- dictionary(lang="en_US", affix=NULL, add_words="bing", 
                              cache=TRUE)

# use hunspell to identify misspelled words and create a row number column 
#   for later uses
mydf$words_checking <- hunspell(mydf$word, dict=mydict_hunspell)
mydf$row_num <- rownames(mydf)

# unlist the words_checking column and get suggestions for those misspelled 
#   words in another data frame
library(tidyr)
mydf_1 <- unnest(mydf, words_checking)
mydf_1$suggestion <- hunspell_suggest(mydf_1$words_checking)

# extract first suggestion from suggestion column
mydf_1$first_suggestion <- lapply(mydf_1$suggestion, `[[`, 1)

Upvotes: 1

Views: 573

Answers (1)

ThetaFC
ThetaFC

Reputation: 670

You can check the length of each list first before trying to extract the element of interest. Also, I recommend using sapply so that you have a character vector returned, as opposed to another list.

For the first suggestion:

index <- 1
sapply(mydf_1$suggestion, function(x) {if(length(x) < index) {NA} else {x[[index]]}})

And for the second suggestion and so on:

index <- 2
sapply(mydf_1$suggestion, function(x) {if(length(x) < index) {NA} else {x[[index]]}})

This could be wrapped into a larger function with a bit more code if you need to automate...

In theory, you could test with is.null(see How to test if list element exists? ), but I still got the same error trying that approach.

Upvotes: 1

Related Questions