[英]Subset tibble (tidyverse) with indexes (short notation)
我想從僅基於索引的小標題中子集一個數值。 例如,如果我想要第二行和第三列,我想使用tibble[2,3]
。
但是,這將返回tibble
而不是單個數字。
我知道可以使用tibble[2,] %>% pull(3)
但是沒有比data.frame方法更短的選項嗎?
這有兩種方法。 對於數據幀或小標題,這兩種方法都將同樣有效。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.