【问题标题】:RStudio: Is there any way to get a data frame to show the column number using the View() function?RStudio:有没有办法让数据框使用 View() 函数显示列号?
【发布时间】:2021-05-22 21:22:56
【问题描述】:

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

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

感谢您的宝贵时间

【问题讨论】:

    标签: r dataframe rstudio


    【解决方案1】:

    你可以label the columns,就像View displays labels

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

    【讨论】:

    【解决方案2】:

    这是一个选项:

    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 创建

    【讨论】:

    • 这很聪明,谢谢你的帮助,投票赞成。
    猜你喜欢
    • 2020-12-24
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多