2

This is a sample (normally it is 15 x 17) from my dataset entrepreneur imported from Excel with read_excel:

structure(list(Factors = c("Competition", "Cultural Support", "Financing", "High Growth", "Human Capital"), `Baden-Württemberg` = c("0.71", "0.66", "0.81", "0.62", "0.46"), Bayern =c("0.67", "0.66", "0.83", "0.77", "0.49"), Berlin = c("1.00", "0.56", "0.90", "0.82", "0.79"), Brandenburg = c("1.00", "0.55", "0.64", "1.00", "0.77")), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame"))

or in this format:

EntrepreneurIndex
# A tibble: 5 x 5
  Factors          `Baden-Württemberg` Bayern Berlin Brandenburg
  <chr>            <chr>               <chr>  <chr>  <chr>      
1 Competition      0.71                0.67   1.00   1.00       
2 Cultural Support 0.66                0.66   0.56   0.55       
3 Financing        0.81                0.83   0.90   0.64       
4 High Growth      0.62                0.77   0.82   1.00       
5 Human Capital    0.46                0.49   0.79   0.77   

The first column as you can see is consisting of my factor variables. I would like to have the first column be transferred to rownames. I have used codes like

rownames(entrepreneur)  <- entrepreneur[,1]

, which led to the error message error in `.rowNamesDF<-`(x, value = value) : non-valid 'row.names' length Zusätzlich: Warnmeldung: Setting row names on a tibble is deprecated.

I'm unfortunately new to the concept of tibbles. I have tried transforming the data into a data frame as another post https://stackoverflow.com/a/50904626 has adviced with as.data.frame(entrepreneur), but this only lead to the same error message as before.

Going to https://tibble.tidyverse.org/reference/rownames.html adviced me to use

column_to_rownames(entrepreneur, var = "Factors")

This didn't cause an error, but it didn't transform the first column to rownames.

After reading the advice and other posts, I'm now not sure whether you can or cannot transfer the first column of a tibble into a row names column. Preferably I would have the first column a rowname for further analysis (regression, etc.) if that's possible.

aynber
  • 22,380
  • 8
  • 50
  • 63
  • entrepeneur %>% column_to_rownames('Factors') works for me. – deschen Sep 17 '21 at 13:08
  • The function that you are using does exactly what it's supposed to, however if you want to save it to `entrepreneur` you need to add `entrepreneur <-` – koolmees Sep 17 '21 at 13:09
  • Did you load the tidyverse package with library(tidyverse)? – deschen Sep 17 '21 at 13:09
  • Assign the output back to `entrepreneur` i.e `entrepreneur <- entrepreneur %>% column_to_rownames('Factors')` – Ronak Shah Sep 17 '21 at 13:10
  • As @RonakShah and @WietsedeVries mentioned, I didnt assign the output back to my dataset variable. After correcting for this, @deschen 's command `entrepeneur %>% column_to_rownames('Factors')` worked out successfully for me! Thanks a lot for the correction! – Frank_Crunch Sep 17 '21 at 14:02

2 Answers2

0

You can set the rownames like so:

rownames(entrepreneur) <- entrepreneur$Factors

However, if you're planning on running regressions on the data in each row, instead of setting row names, you may want to look into how to make nested tibbles--basically, tibbles where one row consists of tibbles (tibbles in tibbles!). Then you can iterate functions over those nested tibbles with purrr::map(), and e.g. run a regression on each line of data and keep the results all in one tibble.

You can read more about nesting tibbles here: https://tidyr.tidyverse.org/articles/nest.html

  • Hello, thx for the comment. As mentioned above I found another solution. I have thought about your idea before, however trying the code always gave me the error message "Setting row names on a tibble is deprecated." Thx also for the nest literature. I might need it later in my further work! – Frank_Crunch Sep 17 '21 at 14:06
  • No prob. Also the "setting rownames is deprecated" message looks like a warning, not an error. I get that message too but it puts the row names on anyway. – Christopher Belanger Sep 17 '21 at 14:12
  • It's true, when I look at the data via the view command, it puts the column as row names as you say. However, the initial column is additionally still there and when I try to delete it via `entrepreneur <- entrepreneur[,-1]` it not only deletes this column, it also deletes the row names I just created. – Frank_Crunch Sep 17 '21 at 14:26
0

First, you can see the difference

> str(entrepreneur[, 1])
tibble [5 x 1] (S3: tbl_df/tbl/data.frame)
 $ Factors: chr [1:5] "Competition" "Cultural Support" "Financing" "High Growth" ...      

> str(entrepreneur[[1]])
 chr [1:5] "Competition" "Cultural Support" "Financing" "High Growth" ...

Try the code below (using entrepreneur[[1]] rather than entrepreneur[,1])

> `rownames<-`(as.data.frame(entrepreneur[-1]), entrepreneur[[1]])
                 Baden-Wⁿrttemberg Bayern Berlin Brandenburg
Competition                   0.71   0.67   1.00        1.00
Cultural Support              0.66   0.66   0.56        0.55
Financing                     0.81   0.83   0.90        0.64
High Growth                   0.62   0.77   0.82        1.00
Human Capital                 0.46   0.49   0.79        0.77
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81