首页 > 解决方案 > 从数据框中的字符向量中提取第二个观察值

问题描述

c()从数据框中的向量中提取第二个观察值。

我有一些看起来像这样的数据:

                          splitNames
1  , grupo, modelo, s.a.b., de, c.v.
2                         , gymboree
3                           , cerner
4     , stellus, capital, investment
5         , cambium, learning, group
6               , cornell, companies
7                      , the, boeing
8                        , wd-40, co
9                         , glencore
10                    , the, valspar

我想从数据中提取grupo, gymboree, cerner, stellus, cambium, Cornell, the, wd-40,Glencorethe。目前我可以做到,但它只grupo从数据中提取。y %>% mutate(splitNames[[1]][[2]])

数据:

structure(list(splitNames = list(c("", "grupo", "modelo", "s.a.b.", 
"de", "c.v."), c("", "gymboree"), c("", "cerner"), c("", "stellus", 
"capital", "investment"), c("", "cambium", "learning", "group"
), c("", "cornell", "companies"), c("", "the", "boeing"), c("", 
"wd-40", "co"), c("", "glencore"), c("", "the", "valspar"))), class = "data.frame", row.names = c(NA, 
-10L))

标签: r

解决方案


我们可以[在没有匿名函数的情况下使用

sapply(data$splitNames, `[`, 2)
#[1] "grupo"    "gymboree" "cerner"   "stellus"  "cambium"  "cornell"  "the"      "wd-40"    "glencore" "the" 

推荐阅读