在新R版本中以旧版本编写的代码编译问题 [英] Problems compiling code written in old version in new R version

查看:142
本文介绍了在新R版本中以旧版本编写的代码编译问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在新版本的R(4.0.1;平台:x86_64-w64-mingw32 / x64(64位))和RStudio(1.3.959版)中运行命令时遇到问题,在旧版本的R

I am having trouble running commands in new version of R (4.0.1; Platform: x86_64-w64-mingw32/x64 (64-bit)) and RStudio (Version 1.3.959) which worked well in the older version of R.

比方说,我有一个名为Check的表,其中包含10,000多行和100多个变量(分类和数字)。

Let's say, I have a table named Check with more than 10,000 rows and more than 100 variables (categorical and numeric).

如果我尝试调用droplevels命令,得到以下消息。

If I try to invoke the droplevels command, I get the below message.

Check <- droplevels(Check)
Error in .shallow(x, cols = cols, retain.key = TRUE) : 
can't set ALTREP truelength

,下面的方法

Check <- rapply(Check, f = droplevels, classes = "factor", how = "replace")

当我尝试通过定义新级别并将其替换为NA来替换类别变量中的NA时,我得到了下面的消息:

When I try to replace the NA's in a categorical variable by defining a new level and replacing it for NA's, I get the below message:

levels(Check$A) <- c(levels(Check$A), 'unknown.')
# Check$A <- factor(Check$A, levels=c(levels(Check$A), 'unknown.'))
Check$A[is.na(Check$A)] <- 'unknown.'
Error in setalloccol(newx) : can't set ALTREP truelength

当我尝试打开表格时,出现以下消息:

When I try to open the table, I get the below message:

View(Check)
Error in view: can't set ALTREP truelength

我不明白这里出了什么问题。

I do not understand what has seriously gone wrong here. Any idea please?

我尝试过玩

library(tidyverse)
Check <- data.frame(col1 = c(NA, letters[1:10]), col2 = c(NA, NA, 1:8, NA), 
                 col3 = c(NA, letters[1:5], NA, NA, NA, NA, NA))
Test <- Check
Test <- droplevels(Test)
str(Test)
Test2 <- Test[6:11,]
Test2 <- Test2 %>% mutate_if(sapply(Test2, is.character), as.factor)
Test2 <- droplevels(Test2)

上面的方法工作正常,并使用 dput(Test2)收益

The above works fine and using dput(Test2) yields

structure(list(col1 = structure(c(NA, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L), .Label = c("a", "b", "c", "d", "e", "f", "g", 
"h", "i", "j"), class = "factor"), col2 = c(NA, NA, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, NA), col3 = structure(c(6L, 1L, 2L, 3L, 4L, 
5L, 6L, 6L, 6L, 6L, 6L), .Label = c("a", "b", "c", "d", "e", 
"unknown."), class = "factor")), row.names = c(NA, -11L), class = "data.frame")

但是,对于我的数据,我得到了一些东西最终使用dput这样,尽管我没有使用data.table。

However, for my data, I get something like this in the end using dput, though I am not using data.table.

row.names = c(NA, 
-5L), .internal.selfref = <pointer: 0x0000000004f81ef0>, class = c("data.table", 
"data.frame"))

当我成功地尽快完成数据时,我试图模仿我的数据并将其摆出。

I am trying to imitate my data and shall pose it, when I am successful in doing it asap.

推荐答案

下面的示例运行正常,没有任何问题:

The below example runs fine without any problem:

library(tidyverse)
library(data.table)

Check <- data.frame(col1 = c(NA, letters[1:10]), col2 = c(NA, NA, 1:8, NA), 
                    col3 = c(NA, letters[1:5], NA, NA, NA, NA, NA))
Test1 <- Check
dput(Test1)

Test2 <- as.data.table(Check) # Convert to data.table
dput(Test2)


Test1 <- droplevels(Test1)
str(Test1)
Test1 <- Test1 %>% dplyr::mutate_if(sapply(Test1, is.character), as.factor)
Test1 <- Test1 %>% dplyr::mutate_if(sapply(Test1, is.integer), as.numeric)
str(Test1)
Test1 <- droplevels(Test1)

Test2 <- droplevels(Test2)
str(Test2)
Test2 <- Test2 %>% dplyr::mutate_if(sapply(Test2, is.character), as.factor)
Test2 <- Test2 %>% dplyr::mutate_if(sapply(Test2, is.integer), as.numeric)
str(Test2)
Test2 <- droplevels(Test2)

现在来看这个例子:

library(tidyverse)
library(data.table)

Check1 <- data.frame(col1 = c(NA, letters[1:200]), col2 = c(NA, NA, 1:198, NA), 
                 col3 = c(NA, letters[1:195], NA, NA, NA, NA, NA),
                 col4 = c(NA, NA, letters[1:199]), col5 = c(NA, letters[7:206]),
                 col6 = c(NA, NA, letters[1:198], NA),
                 col7 = c(NA, letters[1:197], NA, NA, NA),
                 col8 = c(NA, letters[4:203]),
                 col9 = c(NA, letters[6:205]),
                 col10 = c(letters[1:200], NA),
                 col11 = c(NA, NA, letters[1:197], NA, NA),
                 col12 = c(NA, letters[2:201]),
                 col13 = c(NA, NA, NA, NA, NA, letters[1:196]) )

Check2 <- data.frame(replicate(100,sample(0:1000,201,rep=TRUE)))

Check <- cbind(Check1, Check2)


Test1 <- Check
dput(Test1) 
# dput gives ,row.names = c(NA, -201L), class = "data.frame")

Test2 <- as.data.table(Check)
dput(Test2)
# dput gives row.names = c(NA, -201L), .internal.selfref = <pointer: 0x00000000052e1ef0>, class # = c("data.table", "data.frame"))

# The below block runs without any problem since Test1 is of class data.frame
Test1 <- droplevels(Test1)
str(Test1)
Test1 <- Test1 %>% dplyr::mutate_if(sapply(Test1, is.character), as.factor)
Test1 <- droplevels(Test1)
Test1 <- Test1 %>% dplyr::mutate_if(sapply(Test1, is.integer), as.numeric)
str(Test1)
Test1 <- droplevels(Test1)

# The below block gives problem since Test2 is of class = c("data.table", "data.frame")
Test2 <- droplevels(Test2)
str(Test2)
Test2 <- Test2 %>% dplyr::mutate_if(sapply(Test2, is.character), as.factor)
Test2 <- droplevels(Test2)
Test2 <- Test2 %>% dplyr::mutate_if(sapply(Test2, is.integer), as.numeric)
str(Test2)
Test2 <- droplevels(Test2)

运行前4行的以下错误消息:

I get the following error message running the first 4 lines:

Error in in .shallow(x, cols = cols, retain.key = TRUE) : can't set ALTREP truelength

如果我尝试打开Test2数据框,得到以下消息

If I try to open Test2 data frame, I get the following message

View(Test2)
Error in View : can't set ALTREP truelength

但是,如果是I,请使用

If I, however, delete Test2 using

rm(Test2) 

并运行以下命令,我没有收到任何错误:

and run the following, I do not get any error:

Test2 <- as.data.frame(Test2)
str(Test2)
Test2 <- Test2 %>% dplyr::mutate_if(sapply(Test2, is.character), as.factor)
Test2 <- droplevels(Test2)
Test2 <- Test2 %>% dplyr::mutate_if(sapply(Test2, is.integer), as.numeric)
str(Test2)
Test2 <- droplevels(Test2)



  1. 大小限制是否在data.table中起作用,因为它似乎适用于小数据,并且对上面的示例犹豫不决。

  1. Does size restriction play a role in data.table, since it seems to works for small data and hesitates for the example above.

是否表示每次必须确保将数据另存为类= data.frame的数据框?

Does it mean each time it must be ensured that the data is saved as a data frame with class = "data.frame"?

何时执行此类数据框s会自动转换为data.table,因为在我的真实数据集中,虽然data.table库未加载,但是数据仍保存为data.table?

When do such data frames get converted automatically to data.table, because in my real data set though data.table library is not loaded, despite which, the data is saved as a data.table?

请问任何解释?我已经安装了R 4.0.2,并清理了空间并重新安装了所有软件包和依赖项,而RStudio版本是1.3.959。

Any explanation please? I have R 4.0.2 and swiped the space clean and reinstalled all packages and dependencies fresh, and RStudio version is 1.3.959 .

这篇关于在新R版本中以旧版本编写的代码编译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆