cdbezz
cdbezz

Reputation: 43

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

I've searched on google, here and on youtube but I've not seen anyone ask this, thus I doubt it is possible. But I have a data frame with a decent number of rows (25) and I'm making a correlation plot between the numerical variables and a heat map of Chi-Squared Test p-values between the categorical variables.

To do so I need to refer to the column ID for each relevant variable. I'm just wondering if there is a way to make the View() function display column numbers by default, similar to how there is an index column for row numbers.

Thank you for your time

Upvotes: 1

Views: 874

Answers (2)

Waldi
Waldi

Reputation: 41210

You could label the columns, as View displays labels:

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

enter image description here

Upvotes: 1

the-mad-statter
the-mad-statter

Reputation: 8676

Here is an option:

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)

enter image description here

Created on 2021-02-19 by the reprex package (v1.0.0)

Upvotes: 0

Related Questions