Home > Software design >  I want to draw one element out from a tibble variable in R
I want to draw one element out from a tibble variable in R

Time:05-27

I have a dataframe that consist of multiple variables, and some of them are tibbles. i have variables like:

[ID, name, symbol, category, urls,....,slug]

In this case slug is a tibble and consist of multiple urls of interst such as [Website, facebooksite, twitter,..,reddit]

I would like to draw out only webpage for each asset as a new variable.

So far I figured out I can get the webpage for

ID=1 data$urls[[1]][1,] and for ID=2 crypto.info1$urls[[2]][1,]

output is
A tibble: 1 x 2
  name    url                 
  <chr>   <chr>               
1 website https://bitcoin.org/

but I want for every ID in a new variable. I hope someone can assist me.

CodePudding user response:

With lack of context, you can use a for loop. Since you just want the website you can edit the indexing slightly like how I did in the code below.

Disclaimer: I have not ran this code, if you see an error please let me know.

Also: I know this is with a for loop and not vectorized.

urls <- c()

for(i in 1:length(data$urls)){

urls[i] <- data$urls[[i]][i,2] 
}

If I understand your problem correctly, with the above code urls will be a vector of the websites extracted.

Hope this helps.

CodePudding user response:

If you don't mind the made-up example, here's an alternative approach:

library(tidyverse)

# Made-up data
df <- tribble(~id, ~name, ~url,
        1, "website", "bitcoin.org",
        1, "twitter", "#this",
        2, "website", "bitcoin.net",
        2, "twitter", "#that",
        3, "website", "bitcoin.biz",
        3, "twitter", "#other",
        ) |> 
  nest(slug = -id)

# Pluck the url
df |> mutate(website = map_chr(slug, pluck, "url", 1))
#> # A tibble: 3 × 3
#>      id slug             website    
#>   <dbl> <list>           <chr>      
#> 1     1 <tibble [2 × 2]> bitcoin.org
#> 2     2 <tibble [2 × 2]> bitcoin.net
#> 3     3 <tibble [2 × 2]> bitcoin.biz

Created on 2022-05-26 by the reprex package (v2.0.1)

  • Related