How to create columns from sub-list?

r

#1

Hello,

I want to make different columns from sublist in R
Example:

head(data$product_category_tree)

output:

[1] "[\"Clothing >> Women's Clothing >> Lingerie, Sleep & Swimwear >> Shorts >> Alisha Shorts >> Alisha Solid Women's Cycling Shorts\"]"
[2] "[\"Furniture >> Living Room Furniture >> Sofa Beds & Futons >> FabHomeDecor Fabric Double Sofa Bed (Finish Colo...\"]"             
[3] "[\"Footwear >> Women's Footwear >> Ballerinas >> AW Bellies\"]"     

sam <- strsplit(data$product_category_tree,">>")
head(sam)

output

[[1]]
[1] "[\"Clothing "                            " Women's Clothing "                     
[3] " Lingerie, Sleep & Swimwear "            " Shorts "                               
[5] " Alisha Shorts "                         " Alisha Solid Women's Cycling Shorts\"]"

[[2]]
[1] "[\"Furniture "                                          
[2] " Living Room Furniture "                                
[3] " Sofa Beds & Futons "                                   
[4] " FabHomeDecor Fabric Double Sofa Bed (Finish Colo...\"]"

[[3]]
[1] "[\"Footwear "       " Women's Footwear " " Ballerinas "       " AW Bellies\"]"    

Now I want to extract information as particular levels or make function so I will able to extract information at particular level.

can anyone please suggest me how to make For loops for it or make function ?

Thanks in Advance


#2

Hi @premsheth

Please add the expected output format. An example would be best.


#3

Thank you for your reply but I created function
nth_category <- function(data, level){
sam <- strsplit(data$sub_category,">")
lvl <- list()
for (i in 1:length(sam)) {
lvl[[length(lvl) + 1]] <- sam[[i]][level]
}
lvl
}

df$category1 <- nth_category(df,1)
head(df$category1)

so from category tree example : Clothing >> Women’s Clothing >> Lingerie, Sleep & Swimwear >> Shorts

I can extract and create new column for category 1 which contains only 1st observation means " clothing" only.