首页 > 其他分享 >How many ways of selecting/referring to a column in data.table?

How many ways of selecting/referring to a column in data.table?

时间:2023-06-14 14:55:33浏览次数:60  
标签:return referring ways selecting flights year table data select

Load demo data

library(data.table) flights = fread("https://raw.githubusercontent.com/Rdatatable/data.table/master/vignettes/flights14.csv") flights 

Select one single column

Suppose I want to select the first column year .

flights[, 1]  # return data.table 
flights[, .(year)]  # return data.table 
flights[, c("year")]  # return data.table 
flights[, "year"]  # return data.table 

flights[, year]  ### This would return a vector of the column. The same as flights[[year]] or flights$year 

select_col = "year"; flights[, ..select_col]
select_col = "year"; flights[, select_col, with = F]

Select 2 or more columns

Suppose I want to select the first three columns: year, month, day.

flights[, 1:3]  # return data.table
flights[, c(1, 2, 3)]  # return data.table
flights[, .(year:day)]  ### This is to do math calculation: year ÷ day, cuz data.table will consider it as an expression.
flights[, year:day]  # return data.table 
flights[, .(year, month, day)] # return data.table
flights[, c("year", "month", "day")] # return data.table
select_col = c("year", "month", "day");  flights[, ..select_col]  # return data.table
select_col = c("year", "month", "day"); flights[, select_col, with = F]  # return data.table

# Invert selection columns
flights[, !(year:day)] # return data.table 
flights[, !year:day] ### this will do another different math operation. cuz data.table will consider it as an expression.

标签:return,referring,ways,selecting,flights,year,table,data,select
From: https://www.cnblogs.com/res-daqian-lu/p/17480213.html

相关文章

  • 特定情况下docker run --restart=always重启失效的情况
    这是原cicd中使用的语句 在服务器reboot之后,可以看到服务没有随之重启。 通过dockerps-a--no-trunc可以看到--restart=always被当成arg放在了作为entry-point的脚本后面作为传参 这里做了一个猜想,将--restart=always置于dockerrun正后方,而非镜像名后,修改如下:......
  • Vue项目报错: Component name “xxx“ should always be multi-word vue/multi-word-co
    报错的意思是组件名应该始终是多单词,不应该以单个单词命名组件解决办法1:修改组件名称:例如当前的登陆组件名是login.vue修改成LoginName.vue,组件名需要以驼峰式命名至少两个单词,不一定都得是LoginName.vue可以是NameLogin.vue也可以是LoginNiu.vue总之就是以驼峰式命名......
  • Component name "tag" should always be multi-word.
    Componentname"tag"shouldalwaysbemulti-word.这种错误通常是由于编码规范或代码风格指南中的命名规范没有被遵守所导致的。vue中,我命名一个组件叫tag好像不符合规范,应该怎么命名?在Vue中,如果你要命名一个组件,可以采用驼峰式命名法,即使用多个单词组成的名称,每个单词的首......
  • 问题解决:Component name "xxx" should always be multi-word vue/multi-word-compone
    如题,原因是单个单词命名时语法检测无法通过,可以在导出组件时通过name属性给组件名加一个后缀,比如Component。<script>exportdefault{//当组件名为一个单词时,语法检查是无法通过的,可以设置name的值为2个单词来规避检查。name:'HomeComponent'}<......
  • SQL Server 2022 AlwaysOn新特性之包含可用性组介绍
    由于技术能力有限,文章仅能进行简要分析和说明,如有不对的地方,请指正,谢谢......
  • SQL Server Endpoint 与 镜像、AlwaysOn身份验证
    若要加入 AlwaysOn可用性组 或数据库镜像,服务器实例上必须创建自己专用的“数据库镜像端点”(databasemirroringendpoint)。 此端点用途特殊,专门用于接收来自其他实例的连接。数据库镜像端点使用TCP协议在参与数据库镜像会话或承载可用性副本的实例之间发送和接收消息。 数......
  • sqlserver AlwaysOn同步流程与延迟
    区分异步同步提交的关键就在LogHardened这一步,主副本是否需要确认辅助副本已经完成日志固化后才能提交事务。异步提交模式主副本无须确认该辅助副本是否已经完成日志固化,就可以提交事务。但是,辅助数据库的更新可能会滞后于主数据库,如果发生故障转移,可能会导致某些数据丢失。同步......
  • SQL Server 透明数据加密TDE for AlwaysOn
     上篇留下了一个问题,如何将已启用TDE的数据库加入AG?实际上TDEforAlwaysOn分两种情况:对已在AG中的数据库启用TDE已启用TDE的数据库加入AG(更复杂)注意如果在数据库镜像、日志传送、AlwaysOn中使用TDE,则主从数据库都将被加密,不必显式启用辅助数据库加密,事务日志在它们之间发送时将被......
  • Failover and Failover Modes (Always On Availability Groups)
    一、HowAutomaticFailoverWorks 自动故障转移的工作方式Iftheserverinstancethatishostingthecurrentprimaryreplicaisstillrunning,itchangesthestateoftheprimarydatabasestoDISCONNECTEDanddisconnectsallclients.关闭主库,断开所有客户端连接I......
  • in pyvis I always get this error: "AttributeError: 'NoneType' object has no attr
    inpyvisIalwaysgetthiserror:"AttributeError:'NoneType'objecthasnoattribute'render'"SolutionWheninitializingtheNetwork,Iaddednotebook=True,thisfixedtheissueforme.Thenewcodeis:frompyvisimportne......