在R中对数据帧列进行子集设置/索引时,如何处理TIBLES?
我目前正在对我的dataframe列进行如下子集在R中对数据帧列进行子集设置/索引时,如何处理TIBLES?,r,dataframe,indexing,tibble,R,Dataframe,Indexing,Tibble,我目前正在对我的dataframe列进行如下子集df_subset索引tible与索引data.frames相同,只是data.frames试图返回尽可能低的维度,因此存在以下差异: library(tibble) df = data.frame(Measurement = c(2752,2756,2756,2740,2724,2536,2796,2800)) df_tib = as.tibble(df) index = c(2,3,6,7) 索引数据帧: df[index,] # [1]
df_subset索引tible
与索引data.frame
s相同,只是data.frame
s试图返回尽可能低的维度,因此存在以下差异:
library(tibble)
df = data.frame(Measurement = c(2752,2756,2756,2740,2724,2536,2796,2800))
df_tib = as.tibble(df)
index = c(2,3,6,7)
索引数据帧:
df[index,]
# [1] 2756 2756 2536 2796
df_tib[index,]
# A tibble: 4 x 1
# Measurement
# <dbl>
# 1 2756
# 2 2756
# 3 2536
# 4 2796
要在索引后获得向量,实际上需要对列度量值
进行索引。这与data.frame
或tibble
完全相同:
df$Measurement[index]
# [1] 2756 2756 2536 2796
df_tib$Measurement[index]
# [1] 2756 2756 2536 2796
专门用于从TIBLE获取列,可以使用pull()
:
df[c(8,25,23,47),]
?谢谢!这肯定有帮助。所以我编辑了我的描述,因为我的措辞令人困惑。我不介意直接使用TIBLE,但我希望输出是一个向量/易于使用的东西。还有什么方法可以将您所做的转换为向量吗?rowsy如果TIBLE导致问题,您可以使用as.data.frame()
将其转换回数据帧。。。
df[index,, drop=FALSE]
# Measurement
# 2 2756
# 3 2756
# 6 2536
# 7 2796
df$Measurement[index]
# [1] 2756 2756 2536 2796
df_tib$Measurement[index]
# [1] 2756 2756 2536 2796
tb %>% pull(2)
tb %>% pull(Measurement)