试试此代码
f <- factor(sample.int(1000, 100))
df <- data.frame(F = f)
View(df)
现在,当您将鼠标悬停在RStudio View()选项卡中的F列上时,它会显示“具有1个级别的因子”。但是当你分别看f的定义时,它会有所有级别。我试过不同的水平。它显示正确,直到64级后,它表现得很奇怪。请让我知道如何解决这个问题。我已经尝试过“无效”的“nmax”。
下面的代码正常工作
f <- factor(sample.int(1000, 64))
df <- data.frame(F = f)
View(df)
答案 0 :(得分:0)
工作正常。在levels()
和str()
f <- factor(sample.int(1000, 100))
df <- data.frame(F = f)
levels(df$F)
# [1] "7" "8" "15" "20" "22" "33" "51" "65" "70" "84" "86" "93"
# [13] "98" "134" "161" "164" "169" "170" "189" "200" "203" "215" "218" "223"
# [25] "234" "245" "249" "268" "279" "292" "296" "313" "328" "344" "347" "355"
# [37] "357" "363" "370" "376" "388" "392" "394" "397" "441" "449" "470" "487"
# [49] "493" "510" "520" "530" "546" "554" "565" "575" "577" "578" "579" "592"
# [61] "597" "602" "605" "616" "618" "621" "623" "638" "645" "646" "670" "681"
# [73] "694" "695" "716" "717" "723" "725" "734" "740" "759" "775" "793" "794"
# [85] "797" "818" "821" "825" "846" "851" "863" "865" "885" "888" "928" "951"
# [97] "958" "977" "993" "994"
length(levels(df$F))
# [1] 100
str(df)
# 'data.frame': 100 obs. of 1 variable:
# $ F: Factor w/ 100 levels "7","8","15","20",..: 45 80 34 21 74 92 76 78 47 43 ...
View(df)
df$F
# [1] 441 740 344 203 695 865 717 725 470 394 376 347 885 370 84 928 863 618 851 681 65 93 15 621 279 245 554 388
# [29] 449 134 565 888 825 623 223 292 170 670 793 392 723 161 249 397 487 794 7 22 994 602 33 357 215 977 86 577
# [57] 694 296 328 958 993 363 20 716 638 169 759 546 846 646 821 313 70 605 510 218 578 189 98 234 645 200 355 592
# [85] 164 818 597 51 734 8 268 951 575 616 579 530 493 520 797 775
# 100 Levels: 7 8 15 20 22 33 51 65 70 84 86 93 98 134 161 164 169 170 189 200 203 215 218 223 234 245 249 268 ... 994
答案 1 :(得分:0)
这不是R bug而是RStudio错误。这一切都正确地看nlevels()或str()并检查RStudio中的Environment窗格(它对我说因子w / 314级别),但是你注意到的那个错误就是你鼠标悬停/工具提示时View()中的列名称,它表示“具有1个级别的因子”
截至2017年2月,有人报告了这个漏洞并且RStudio正在研究它。 https://support.rstudio.com/hc/en-us/community/posts/115001084508-Factor-levels-differ-in-view-mode-and-environment-tab?input_string=View()%20bug%20when%20viewing%20factors%20with%20%3E64%20levels