首页 > 解决方案 > 使用`lapply`和`grepl`修剪变量内的嵌套列表

问题描述

我正在处理抓取的数据,并且正在使用一个被拆分为嵌套列表的变量用于多种用途。这是我想要做的:

  1. 在嵌套列表中查找与字符串“Highlights”完全匹配的项目
  2. 识别并删除所有包含冒号 ( :)的嵌套列表项
  3. 识别并删除所有超过 30 个字符的嵌套列表项
  4. 重新排序列表,使“突出显示”位于顶部
  5. 在 HTML 中包装不是“突出显示”的列表项(<li>在嵌套列表项之前和</li>之后)。
  6. <ul>取消列出,并在字符串的开头和结尾添加包装器,以便将列表格式化为 HTML

这是一个示例: 原始文本

[1] "Phthalate Free"
[2] "Paraben Free"
[3] "Sulfate Free"
[4] "Highlights"
[5] "Reduces Pores" 
[6] "The Peach Slices Shrink Pores Mask contains exfoliating willow bark, soothing hinoki cypress, hydrating rose, brightening citrus, clarifying cinnamon and other natural ingredients to help firm skin and reduce the appearance of pores. This natural cotton sheet mask will nourish and pamper your skin with the hydration it needs.
[7] "TCIN: 53659598"
[8] "UPC: 818000020054"
[9] "Item Number (DPCI): 037-14-1205"

取消列出前所需的文本

[1] "Highlights"
[2] "<li>Phthalate Free</li>"
[3] "<li>Paraben Free</li>"
[4] "<li>Sulfate Free</li>"
[5] "<li>Reduces Pores</li>"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

最终结果 [1] ""<ul>突出显示<li>不含邻苯二甲酸盐 不含</li><li>防腐剂 不含</li><li>硫酸盐</li><li>减少毛孔</li></ul>"

我有一些非常丑陋lapply的功能,我正在努力逐步进行这些转换,但我希望有一个优雅的解决方案。

此处提供了更多测试数据:

dput(head(tgt$clean.text))
list(c("Phthalate Free", "Paraben Free", "Sulfate Free", "No Added Fragrance", 
"Highlights", "Made for Young Delicate Skin Types", "Mineral pressed powder foundation blends easily to give skin natural light to medium coverage", 
"Clinically Tested on Sensitive Skin", "Dermatologist Tested, Hypoallergenic, Fragrance-Free, Talc-Free", 
"NO - Talc, Fragrance, Parabens, Phthalates, Sulfates, Mineral oil or Propylene Glycol", 
"This pressed mineral powder foundation offers buildable, light to medium coverage with a unique sheer formula designed to conceal without appearing heavy or caked on.", 
"Health Facts: Sulfate-free, Talcum-free, butylparaben-free, formaldehyde donor-free, formaldehyde-free, hypoallergenic, mineral oil-free, no fragrance added, non-toxic, paraben-free, phthalate-free, propylparaben-free, sodium lauryl sulfate-free, nonylphenol ethoxylate free, propylparaben-free, aluminum-free", 
"Color Family: Beige", "Color Palette: Light Tones", "Product Form: Pressed Powder", 
"Suggested Age: 8 Years and Up", "Cosmetic Coverage: Medium", 
"Skin Tone: Medium", "Wellness Standard: Meets minimum requirements, no fragrance added, paraben-free, phthalate-free, sulfate-free", 
"Recommended Skin Type: Normal", "Beauty Purpose: Color correction", 
"Features: Blendable, Lightweight, Unscented, dermatologist tested", 
"TCIN: 53070801", "UPC: 039897626147", "Item Number (DPCI): 037-14-1089", 
"Phthalate Free", "A product that either carries an unqualified on-pack statement indicating that the product is free from phthalates, or carries an unqualified on-pack statement \"no phthalates,\" or A product which is never formulated with phthalates, as confirmed by the manufacturer.", 
"Paraben Free", "A product that either carries an unqualified on-pack statement indicating that the product is free from parabens, or carries an unqualified on-pack statement \"no parabens,\" or A product which is never formulated with parabens, as confirmed by the manufacturer.", 
"Sulfate Free", "A product that either makes an unqualified on-pack statement indicating that it does not contain sulfates (not added sulfates); or carries an unqualified on-pack statement \"no sulfates\" or \"sulfate-free\".", 
"No Added Fragrance", "A product that either makes an on-pack statement indicating that it does not contain synthetic fragrance, or carries an unqualified on-pack statement \"no fragrance\".", 
"If the item details above are not accurate or complete, we want to know about it. Report incorrect product info."
), c("Phthalate Free", "Paraben Free", "Sulfate Free", "Cruelty Free", 
"No Added Fragrance", "Highlights", "Reduces Pores", "Deep hydration", 
"Contains willow bark0.84 fl oz", "The Peach Slices Shrink Pores Mask contains exfoliating willow bark, soothing hinoki cypress, hydrating rose, brightening citrus, clarifying cinnamon and other natural ingredients to help firm skin and reduce the appearance of pores. This natural cotton sheet mask will nourish and pamper your skin with the hydration it needs. Say goodbye to your dead skin cells and hello to fresh, radiant skin with the Peach Slices Shrink Pores Mask\\!Scent: Fresh", 
"Health Facts: Sulfate-free, paraben-free, dye-free, aluminum-free, phthalate-free, no fragrance added", 
"Product Warning: Adult Use Only", "Product Form: Sheet", "Recommended Skin Type: Normal", 
"Beauty Purpose: Pore reduction", "Skin concern: Visible pores", 
"Sustainability Claims: Cruelty-free", "TCIN: 53659598", "UPC: 818000020054", 
"Item Number (DPCI): 037-14-1205", "Healthcare Disclaimer: Content on this site is for reference purposes only.  Target does not represent or warrant that the nutrition, ingredient, allergen and other product information on our Web or Mobile sites are accurate or complete, since this information comes from the product manufacturers.  On occasion, manufacturers may improve or change their product formulas and update their labels.  We recommend that you do not rely solely on the information presented on our Web or Mobile sites and that you review the product's label or contact the manufacturer directly if you have specific product concerns or questions.  If you have specific healthcare concerns or questions about the products displayed, please contact your licensed healthcare professional for advice or answers.", 
"Phthalate Free", "A product that either carries an unqualified on-pack statement indicating that the product is free from phthalates, or carries an unqualified on-pack statement \"no phthalates,\" or A product which is never formulated with phthalates, as confirmed by the manufacturer.", 
"Paraben Free", "A product that either carries an unqualified on-pack statement indicating that the product is free from parabens, or carries an unqualified on-pack statement \"no parabens,\" or A product which is never formulated with parabens, as confirmed by the manufacturer.", 
"Sulfate Free", "A product that either makes an unqualified on-pack statement indicating that it does not contain sulfates (not added sulfates); or carries an unqualified on-pack statement \"no sulfates\" or \"sulfate-free\".", 
"Cruelty Free", "A product has an independent third-party certification on pack or unqualified on-pack marketing claim relating to being cruelty-free and/or not tested on animals.", 
"No Added Fragrance", "A product that either makes an on-pack statement indicating that it does not contain synthetic fragrance, or carries an unqualified on-pack statement \"no fragrance\".", 
"If the item details above are not accurate or complete, we want to know about it. Report incorrect product info."
), c("Highlights", "Soft, refreshing texture", "For sensitive skin", 
"La Roche-Posay Toleriane Riche Soothing Protective Face Cream for Dry, sensitive and very sensitive skin. Formulated with Shea Butter to nourish and replenish skin's surface with lipids, and with La Roche-Posay Thermal Water to help soothe skin. This face cream contains a limited number of ingredients designed with the tolerance of sensitive skin in mind.\n\nHow to Use: Use daily. After mild cleansing, apply this moisturizer to the face and neck morning and evening.", 
"Scent: Unscented", "Health Facts: Paraben-free, no fragrance added", 
"Product Form: Cream", "Recommended Skin Type: Dry, normal, sensitive, mature", 
"Beauty Purpose: Deep hydration, moisturizing, spot treatment", 
"Skin concern: Dark spots, dry skin, fine lines", "Features: Dermatologist tested", 
"TCIN: 14225616", "UPC: 3433422405295", "Item Number (DPCI): 037-14-0282", 
"Healthcare Disclaimer: Content on this site is for reference purposes only.  Target does not represent or warrant that the nutrition, ingredient, allergen and other product information on our Web or Mobile sites are accurate or complete, since this information comes from the product manufacturers.  On occasion, manufacturers may improve or change their product formulas and update their labels.  We recommend that you do not rely solely on the information presented on our Web or Mobile sites and that you review the product's label or contact the manufacturer directly if you have specific product concerns or questions.  If you have specific healthcare concerns or questions about the products displayed, please contact your licensed healthcare professional for advice or answers.", 
"If the item details above are not accurate or complete, we want to know about it. Report incorrect product info."
), c("Highlights", "Daily anti-aging primer with sunscreen protection.", 
"For everyday use. Matte finish texture.", "Primer with sunscreen, provides blurring-effect on skin.", 
"For daily use with or without makeup", "La Roche-Posay Anthelios Daily Anti-Aging Face Primer with sunscreen SPF 50 instantly provides a visible blurring-effect on signs of aging: fine lines, wrinkles and pores. For everyday use. Matte finish texture. The Anti-Aging Face Primer with Sunscreen provides a blurring-effect on skin. For daily use with or without makeup.\n\nHow to Use: Apply each morning to clean face and neck; 15 minutes before sun exposure. Use as last step in skincare routine.", 
"Scent: Unscented", "Health Facts: Noncomedogenic, no fragrance added, paraben-free", 
"Product Form: Cream", "SPF: 50", "Recommended Skin Type: Normal", 
"Beauty Purpose: Sun protection, anti-aging, smoothing, pore reduction, color correction, brightening", 
"Skin concern: Aging, skin health, sun protection, sensitive skin", 
"TCIN: 14225596", "UPC: 883140020950", "Item Number (DPCI): 037-14-0098", 
"Healthcare Disclaimer: Content on this site is for reference purposes only.  Target does not represent or warrant that the nutrition, ingredient, allergen and other product information on our Web or Mobile sites are accurate or complete, since this information comes from the product manufacturers.  On occasion, manufacturers may improve or change their product formulas and update their labels.  We recommend that you do not rely solely on the information presented on our Web or Mobile sites and that you review the product's label or contact the manufacturer directly if you have specific product concerns or questions.  If you have specific healthcare concerns or questions about the products displayed, please contact your licensed healthcare professional for advice or answers.", 
"If the item details above are not accurate or complete, we want to know about it. Report incorrect product info."
), c("Highlights", "Hydromoist balancing cream is ideal for dry and sensitive skin", 
"Contains Bio Science Complex technology that promotes fast and deep absorption, delivering active components to skin", 
"Strengthens skin's protective layer", "Oil-free and non-sticky cream provides long-lasting hydration", 
"This Hydromoist Balancing Cream from Derma Dr.", "Lab helps to restore the natural balance in your skin. The nutrient-rich cream provides moisture to your skin, while controlling the skin's oil-water balance and providing a long-lasting moisture benefits year-round, which is particularly necessary for those in dry and arid climates. The balancing cream also helps to avoid loss of moisture by reinforcing the skin's protective shield, helping you retain a healthy-looking glow.", 
"Scent: Fresh", "Product Form: Cream", "Recommended Skin Type: Combination, dry, normal, sensitive", 
"Beauty Purpose: Anti-aging, moisturizing, skin tone improvement, fine line reduction, skin texture improvement", 
"Skin concern: Aging, basic care, dry skin, fine lines", "Features: Dermatologist tested", 
"TCIN: 50580868", "UPC: 8809112139903", "Item Number (DPCI): 037-14-0609", 
"Healthcare Disclaimer: Content on this site is for reference purposes only.  Target does not represent or warrant that the nutrition, ingredient, allergen and other product information on our Web or Mobile sites are accurate or complete, since this information comes from the product manufacturers.  On occasion, manufacturers may improve or change their product formulas and update their labels.  We recommend that you do not rely solely on the information presented on our Web or Mobile sites and that you review the product's label or contact the manufacturer directly if you have specific product concerns or questions.  If you have specific healthcare concerns or questions about the products displayed, please contact your licensed healthcare professional for advice or answers.", 
"If the item details above are not accurate or complete, we want to know about it. Report incorrect product info."
), c("Highlights", "Moisturizer with glycerin replenishes and retains moisture for smooth skin", 
"Loaded with vitamins B2 and E that soothe and heal skin", "Non-greasy formula dries quickly", 
"Fragrance-free moisturizer with natural ingredients is great for those with allergies or sensitivities to smells", 
"Treat dry skin in a natural way with Moisturizing Skin Cream from Yu-Be. This fast-absorbing moisturizer is loaded with highly concentrated plant-based glycerin and vitamins B2 and E that replenish and retain moisture while nourishing and healing skin, leaving it smooth and velvety soft. The non-greasy formula also contains camphor that soothes dry and patchy skin, making it effective for use anywhere on the body including the face and lips.", 
"Scent: Unscented", "Suggested Age: All Ages", "Recommended Skin Type: Dry", 
"Beauty Purpose: Moisturizing", "TCIN: 51251940", "UPC: 850353000338", 
"Item Number (DPCI): 037-14-0716", "If the item details above are not accurate or complete, we want to know about it. Report incorrect product info."
))

标签: rlapply

解决方案


如果我们在上面调用您的列表search.list,那么这是否符合您的要求:

> sapply(search.list, function(x) {
+   # Look for items within the nested list that have an exact match with the string "Highlights"
+   highlights <- which(x == "Highlights")
+   
+   # Identify and remove all nested list items that contain a colon (:)
+   x <- x[-grep(":", x)]
+   
+   # Identify and remove all nested list items with more than 30 characters
+   x <- x[nchar(x) <= 30]
+   
+   # Re-order the list so "Highlight" is at the top
+   x <- c("Highlights", x[-highlights])
+   x <- x[!duplicated(x)]
+   
+   # Wrap the list items that are not "Highlight" in HTML (with <li> before the nested list item, and </li>behind it).
+   x[-1] <- paste("<li>", x[-1], "</li>")
+   
+   # Unlist, and add <ul> wrappers at the beginning and end of the string so the list is formatted for HTML
+   x <- paste(x, collapse = " ")
+   paste("<ul>", x, "</ul>")
+ })
[1] "<ul> Highlights <li> Phthalate Free </li> <li> Paraben Free </li> <li> Sulfate Free </li> <li> No Added Fragrance </li> </ul>"                                                                                                                     
[2] "<ul> Highlights <li> Phthalate Free </li> <li> Paraben Free </li> <li> Sulfate Free </li> <li> Cruelty Free </li> <li> No Added Fragrance </li> <li> Reduces Pores </li> <li> Deep hydration </li> <li> Contains willow bark0.84 fl oz </li> </ul>"
[3] "<ul> Highlights <li> Soft, refreshing texture </li> <li> For sensitive skin </li> </ul>"                                                                                                                                                           
[4] "<ul> Highlights </ul>"                                                                                                                                                                                                                             
[5] "<ul> Highlights </ul>"                                                                                                                                                                                                                             
[6] "<ul> Highlights </ul>" 

推荐阅读