RStudio:有没有办法使用 View() 函数获取数据框以显示列号?

RStudio: Is there any way to get a data frame to show the column number using the View() function?

我已经在 google、此处和 youtube 上进行了搜索,但我没有看到有人问过这个问题,因此我怀疑这是否可能。 但是我有一个包含相当多行数 (25) 的数据框,我正在绘制数值变量之间的相关图和分类变量之间卡方检验 p 值的热图。

为此,我需要参考每个相关变量的列 ID。 我只是想知道是否有办法让 View() 函数默认显示列号,类似于行号的索引列。

感谢您的宝贵时间

这里有一个选项:

set.seed(42)

# example dataframe
df_0 <- data.frame(x = rnorm(3),
                   y = rnorm(3),
                   z = rnorm(3))
# copy to leave original copy intact
df_1 <- df_0
# rename columns with indices
colnames(df_1) <- 1:ncol(df_1)
# produces
df_1
#>            1          2           3
#> 1  1.3709584  0.6328626  1.51152200
#> 2 -0.5646982  0.4042683 -0.09465904
#> 3  0.3631284 -0.1061245  2.01842371

View(df_1)

reprex package (v1.0.0)

于 2021-02-19 创建

你可以label the columns, as View displays labels:

library(labelled)
var_label(iris) <- as.list(setNames(as.character(1:ncol(iris)),colnames(iris)))
View(iris)