在data.table上使用tail

时间:2021-08-09 20:10:42

I'd like to use tail on a data.table like I do on a data frame. Is that possible? If not what's more efficient way to get the tail of a data table?

我想在data.table上使用tail,就像我在数据框上一样。那可能吗?如果不是什么更有效的方式来获取数据表的尾部?

mainDF= data.frame(name= c("a","b","c","d"))
tail(mainDF,1)["name"]
dt<-as.data.table(mainDF)
setkey(dt, name )
tail(dt,1)["name"]

output

> mainDF= data.frame(name= c("a","b","c","d"))
> tail(mainDF,1)["name"]
  name
4    d
> dt<-as.data.table(mainDF)
> setkey(dt, name )
> tail(dt,1)["name"]
   name
1:   NA

1 个解决方案

#1


Please have a look at the new HTML vignettes, especially the Introduction to data.table vignette, which explains how to subset columns the data.table way, and the rationale behind it.

请查看新的HTML插图,特别是data.table插图简介,它解释了如何将列以data.table的方式进行子集化,以及它背后的基本原理。

The answer is to use with=FALSE, as @thelatemail points out in comments.

答案是使用= FALSE,正如@thelatemail在评论中指出的那样。

tail(dt, 1L)[, "name", with=FALSE]

#1


Please have a look at the new HTML vignettes, especially the Introduction to data.table vignette, which explains how to subset columns the data.table way, and the rationale behind it.

请查看新的HTML插图,特别是data.table插图简介,它解释了如何将列以data.table的方式进行子集化,以及它背后的基本原理。

The answer is to use with=FALSE, as @thelatemail points out in comments.

答案是使用= FALSE,正如@thelatemail在评论中指出的那样。

tail(dt, 1L)[, "name", with=FALSE]

相关文章