Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does data.table, setting "with" = F, output a data.table when j is a single column?

Tags:

r

data.table

Let's say I have data table dt.

dt = as.data.table(c(0,1,2,3))

I'd like to know why the behavior of dt differs in the following two lines of code.

dt[,V1]

dt[,"V1",with=F]

Specifically, the first line produces a numeric vector while the second line produces a data.table.

I'd like to build a function that would allow me to retrieve single columns dynamically by passing a string (thus with=F) and use that output in certain other functions. As it is now, the behavior in the latter case can cause an error in some functions, such as ecdf and hist, which don't accept a data.frame or data.table.

Here's a workaround I've made.

as.data.frame(dt[,"V1",with=F])[,1]

This returns the expected output: a vector which plays nicely with ecdf and hist. It's just a bit messy. Is there any reason why the behavior in dt[,"V1",with=F] differs from dt[,V1]?

like image 759
Alexander Li Avatar asked Mar 12 '15 16:03

Alexander Li


People also ask

What is the one purpose of a data table?

Using data tables makes it easy to examine a range of possibilities at a glance. Because you focus on only one or two variables, results are easy to read and share in tabular form. A data table cannot accommodate more than two variables. If you want to analyze more than two variables, you should instead use scenarios.

What does the data table () function provide to big data processing?

The data. table package provides a faster implementation of the merge() function. The syntax is pretty much the same as base R's merge() .

How do I add a column to a data table in R?

A column can be added to an existing data table using := operator. Here ':' represents the fixed values and '=' represents the assignment of values. So, they together represent the assignment of fixed values. Therefore, with the help of “:=” we will add 2 columns in the above table.

How do I convert a Dataframe to a data table in R?

Method 1 : Using setDT() method table package, which needs to be installed in the working space. The setDT() method can be used to coerce the dataframe or the lists into data. table, where the conversion is made to the original dataframe. The modification is made by reference to the original data structure.


1 Answers

According to the data.table FAQ, many users of data.frame experienced errors when they returned a single column from a data.frame without setting drop=FALSE. These users expected a single column data.frame output rather than a vector. The default behavior of data.table when with=FALSE conforms to these users' expectations.

like image 107
Alexander Li Avatar answered Nov 15 '22 09:11

Alexander Li