重命名列表而不覆盖R中的数据
我希望在不更改基础数据的情况下,将嵌套列表名称重命名为相同的名称 以下是我的示例列表:重命名列表而不覆盖R中的数据,r,R,我希望在不更改基础数据的情况下,将嵌套列表名称重命名为相同的名称 以下是我的示例列表: #Create dataframes ABC.123 and ABC.987, where 'left' and 'right' columns are numeric and the 'up' column contains characters ABC.123 <- cbind.data.frame(left = c(2, 3, 5), right = c(5, 8, 4) , up = c(&qu
#Create dataframes ABC.123 and ABC.987, where 'left' and 'right' columns are numeric and the 'up' column contains characters
ABC.123 <- cbind.data.frame(left = c(2, 3, 5), right = c(5, 8, 4) , up = c("aa","aa", "aa"))
ABC.123$up <- as.character(ABC.123$up)
ABC.987 <- cbind.data.frame(left = c(7, 2, 9), right = c(3, 6, 1) , up = c("bb","bb", "bb"))
ABC.987$up <- as.character(ABC.987$up)
#Create a list called mylist, containing two dataframes: ABC.123 and ABC.987
mylist <- list(ABC.123 = ABC.123, ABC.987 = ABC.987)
$ABC.123
# left right up
#1 2 5 aa
#2 3 8 aa
#3 5 4 aa
$ABC.987
# left right up
#1 7 3 bb
#2 2 6 bb
#3 9 1 bb
通过简单的名称更改,基础数据将发生更改,我试图避免这种情况,当我在RStudio环境窗口中单击mylist时,我会看到:
names(mylist) <- c("ABC", "ABC")
mylist
$ABC
# left right up
#1 2 5 aa
#2 3 8 aa
#3 5 4 aa
$ABC
# left right up
#1 2 5 aa
#2 3 8 aa
#3 5 4 aa
names(mylist)根据@user2554330和@joran的评论,我试图在我的问题中完成的内容在RStudio中引发了一个众所周知的、有据可查的错误:
在这种情况下,请信任控制台输出,它准确地反映了对数据对象的任何操作。要知道,在RStudio环境窗格中看到的不是数据对象的精确表示
总的来说,不要将列表或嵌套列表重命名为非唯一/重复的标识符,以避免在一开始就提示错误。是否重新检查了新的R会话?正在运行names(mylist)@joran,是的,我刚刚完全重新启动了RStudio。当我重新运行我的示例中的代码时,我在全局环境中单击mylist,我看到上面示例中的错误覆盖列表。但当我在控制台窗口中键入“mylist”时,它会返回正确的重新命名。那么,哪一个是正确的,为什么它们不显示相同的结果呢?好的,我看到错误行为的唯一地方是,如果我在“环境”窗格中单击对象,然后在“脚本编辑器”窗格中打开一个新选项卡,如果我在其中单击单个元素,我会看到错误的数据显示。这绝对只是RStudio中的一个UI错误。控制台是正确的。对象就是您想要的。这是RStudio UI的一个问题。我们没有办法“修复”它;您可以将其作为错误报告给他们。这实际上是RStudio中已知的旧错误:。你该怎么办?如果你认为它对任何人都没有帮助,你可以删除它,但我认为应该保留它。所以你可以写一个指向这个bug报告的答案,或者如果其他人写了,你可以接受他们的答案。
names(mylist) <- c("ABC", "ABC")
mylist
$ABC
# left right up
#1 2 5 aa
#2 3 8 aa
#3 5 4 aa
$ABC
# left right up
#1 2 5 aa
#2 3 8 aa
#3 5 4 aa