[英]Colon-Equals operator proper usage
I used the := in R to perform some manipulations in my data set but the usage which I am implementing throws an error.我使用 R 中的 := 在我的数据集中执行一些操作,但我正在实施的用法会引发错误。
I tried using other functions like c() for creating subsets but I need something efficient and apparently := should do the job for me.我尝试使用 c() 等其他函数来创建子集,但我需要一些有效的方法,显然 := 应该为我完成这项工作。 With the subset function, I have a lot of intermediate data frames which are of course unnecessary.使用子集功能,我有很多中间数据帧,这些当然是不必要的。
#preprocessing steps for getting rid of the null values rows
df_data[Quantity<=0,Quantity:=NA]
df_data[UnitPrice<=0,UnitPrice:=NA]
df_data <- na.omit(df_data)
(from the console): (来自控制台):
> df_data[Quantity<=0,Quantity:=NA]
Error in `:=`(Quantity, NA) :
Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").
:=
only works in data.tables :=
仅适用于 data.tables
This should work这应该工作
df_data <- data.table(Quantity = -5:5)
df_data[Quantity<=0,Quantity:=NA]
na.omit(df_data)
This will produce the error这将产生错误
df_data <- data.frame(Quantity = -5:5)
df_data[Quantity<=0,Quantity:=NA]
na.omit(df_data)
That said if you're just filtering out values less than 0 you could do也就是说,如果您只是过滤掉小于 0 的值,您可以这样做
df_data <- df_data[Quantity > 0 & UnitPrice > 0]
Fixed the problem now by using fread instead of read.csv while loading the dataset and it works with the := function.现在通过在加载数据集时使用 fread 而不是 read.csv 解决了该问题,并且它与 := 函数一起使用。
Also, here, posting a useful link for understanding fread and read.csv:另外,在这里,发布一个有用的链接来理解 fread 和 read.csv:
Reason behind speed of fread in data.table package in R R中data.table包中fread速度背后的原因
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.