Extract data in a list in a data frame

Hi, I want to extract values in a list, where those list is stored in a dataframe.

Data
df is a dataframe of 1000 rows and 20 column. I only interested in the 7th column (a list), so ignore another column.

Here's the first row of the df[[7]] looks like (or its df[[7]][[1]]):

What I want to do
I want to extract the screen_name value under user_mention which in this case paijodirajo. Except, I want to get all rows screen_name data instead of only first row.

I'm using this stupid code df[[7]][[1:10000]]$user_mentions$screen_name which ofcourse failed, but you get the idea.

So how to do it correctly?

Since I'm struggling to address the location of the value, creating a reproducible data is even more challenging for me. But right, this question is really messy. I'll try to fix the question. Thanks for reminding

here iris stands in for df, as I dont have df to play with. but everyone who uses r can acces iris:
while you wouldnt expect to be able to do iris[[4]][[1:5]] you could do iris[[4]][1:5]
this gets the first 5 columnwise entries in the 4th column of iris.

1 Like

I feel like it's not very accurate to use iris as an example because my dataframe structure is very different. Its the output of rtweet::search_tweets() function, so my df have a lot of column that contains lists.
The 7th column contains list which data looked like this


That list structure is for each row in column 7 (the example on the pic is first row).

However, I managed to find the solution. For anyone who looking for answer, heres the solution

screen_name_data <- vector()
for (i in 1:nrow(df)) {
screen_name_data[i] <- df[[7]][[i]][["user_mentions"]][["screen_name"]]
}

With this loop method, I managed to get specific values in each row that I want.

print(screen_name_data)

#> [1] "paijodirajo"     NA                NA               
#> [4] "P3n99u94t"       "bianca125547620" "MichelAdam9191" 
#> [7] "OposisiCerdas"   "hc_poirot"       "MichelAdam9191" 
#> 10] "bianca125547620" "MichelAdam9191"  "MichelAdam9191"
#> ...and so on

Anyway, thanks for the reply

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.