我想从仅基于索引的小标题中子集一个数值。例如,如果我想要第二行和第三列,我想使用tibble[2,3]
。
但是,这将返回tibble
而不是单个数字。
我知道可以使用tibble[2,] %>% pull(3)
,但是没有比data.frame方法更短的选项了吗?
答案 0 :(得分:2)
有两种方法。对于数据帧或小标题,这两种方法都同样适用。
library(tibble)
x = as_tibble(mtcars)
## The problem
x[1, 1]
## A tibble: 1 x 1
# mpg
# <dbl>
# 1 21
## Solution 1: [.data.frame has drop = TRUE by default. Tibble switches
## the default to drop = FALSE, but you can still use the argument:
x[1, 1, drop = TRUE]
# [1] 21
## Solution 2: Use [[ to get a single column as a vector, and [ to
## pull the element you want
x[[1]][1]
[1] 21