Home > database >  Pivot_longer (dplyr) in R without row names
Pivot_longer (dplyr) in R without row names

Time:03-02

I have a problem in my analysis and i am stuck.I have a resulted tibble that is :

> corr
# A tibble: 1 x 138
  A       B     C     D     E     F     G     H      I      J
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>
1 0.625 0.393 0.771 0.405 0.636 0.249 0.372 0.154 -0.112  0.293
# ... with 128 more variables:

I want to pivot longer it but i don't have any row specific name if order to start it.

I am using :

corr%>%pivot_longer( names_to = "income", values_to = "count")

but R reports me an error :

Error in `build_longer_spec()`:
! `cols` must select at least one column.

I tried to transpose it and works fine but i cannot extract the names any further.

I think that pivot_longer will transpose it and keep the names of the tibble structure but i don;t know how to do it . Any help ?

CodePudding user response:

In cols , you can select everything() -

df <- data.frame(A = 5, B = 4, C = 8, D = 10)

tidyr::pivot_longer(df, cols = tidyselect::everything(), 
                    names_to = "income", values_to = "count")

#  income count
#  <chr>  <dbl>
#1 A          5
#2 B          4
#3 C          8
#4 D         10
  • Related