[英]extracting the second observation from a character vector in a data frame
從數據幀中的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
、 Glencore
和the
。 目前我可以做到,但它只從數據中提取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))
根據您的數據結構,您可以使用:
sapply(data$splitNames, function(x) x[2])
[1] "grupo" "gymboree" "cerner" "stellus" "cambium" "cornell"
[7] "the" "wd-40" "glencore" "the"
我們可以使用[
沒有匿名 function
sapply(data$splitNames, `[`, 2)
#[1] "grupo" "gymboree" "cerner" "stellus" "cambium" "cornell" "the" "wd-40" "glencore" "the"
由於您無論如何都在使用 Tidyverse,因此您也可以這樣做:
library(purrr)
map_chr(data$splitNames, 2)
#> [1] "grupo" "gymboree" "cerner" "stellus" "cambium" "cornell"
#> [7] "the" "wd-40" "glencore" "the"
由代表 package (v0.3.0) 於 2020 年 7 月 14 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.