本文介绍了是否以整齐/dplyr的方式从数据帧/Tibble中提取单个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道从DataFrame/Tibble获取单个值的几种方法。
library(dplyr)
start_date <- tibble::tribble(
~StaffAbbrev, ~starting_date,
"Alexander", "2021-08-23",
"Cornelis", "2021-08-23",
"Sotirchos", "2021-08-23",
"Zhao", "2021-08-23",
"Park", "2022-02-14",
"Sarkar", "2022-04-04"
)
#Tidyverse way
Alexander_start_v1 <- start_date %>%
filter(StaffAbbrev == "Alexander") %>%
select(starting_date) %>%
unlist() %>%
unname()
#Base R way
Alexander_start_v2=start_date$starting_date[start_date$StaffAbbrev=="Alexander"]
从数据帧/Tibble中提取单个特定值的快捷/更优雅/一行整齐的方式吗?
推荐答案
以下是一些可能性:
library(dplyr)
library(tibble)
start_date %>% deframe %>% getElement("Alexander")
## [1] "2021-08-23"
library(dplyr)
library(tibble)
start_date %>% deframe %>% .[["Alexander"]]
## [1] "2021-08-23"
library(dplyr)
start_date %>% filter(StaffAbbrev == "Alexander") %>% pull
## [1] "2021-08-23"
library(dplyr)
library(magrittr)
start_date %>% filter(StaffAbbrev == "Alexander") %$% starting_date
## [1] "2021-08-23"
这里是基本R代码
with(start_date, starting_date[match("Alexander", StaffAbbrev)])
## [1] "2021-08-23"
这篇关于是否以整齐/dplyr的方式从数据帧/Tibble中提取单个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!