Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform tibble to data frame with column headers

I have data from an excel sheet imported like this:

F4_Off <- readxl::read_xlsx("myExcel.xlsx", sheet = "Offline", col_names = TRUE, range = "I1:L285")
F4_Off

F4_On <- readxl::read_xlsx("myExcel.xlsx", sheet = "Online", col_names = TRUE, range = "J1:M68")
F4_On

This is combined using:

F4_Gesamt <- rbind(F4_Off, F4_On)

and the data looks like this:

A tibble: 351 x 4
    nein Smartphone `Computer / Laptop / Tablet` `keine Angabe`
   <dbl>      <dbl>                        <dbl>          <dbl>
 1   NA         NA                            1.             NA
 2   NA          1.                          NA              NA
 3   NA          1.                          NA              NA
 4   NA          1.                          NA              NA
 5   NA          1.                           1.             NA
 6   NA          1.                           1.             NA
 7   NA          1.                           1.             NA
 8   NA          1.                          NA              NA
 9   NA         NA                            1.             NA
10    1.        NA                           NA              NA

Actually i want this to be summed up per Column (summed up, count the "1.") and transformed into a table like this:

    Type                                    Value
    <chr>                                   <dbl>
1   nein                                    162
2   Smartphone                              120
3   `Computer / Laptop / Tablet`            93
4   `keine Angabe`                          16

i can achieve the summing up by doing:

F4_Gesamt_sum <- colSums(F4_Gesamt, na.rm = TRUE, dims = 1)

then it looks like this:

                   nein                 Smartphone Computer / Laptop / Tablet               keine Angabe 
                    162                        120                         93                         16 

now:

str(F4_Gesamt_sum)

gives:

Named num [1:4] 162 120 93 16
 - attr(*, "names")= chr [1:4] "nein" "Smartphone" "Computer / Laptop / Tablet" "keine Angabe"

And this is the point where i fails miserably for days now, how do i get it to look like I "faked" it above?

At the end I want to feed this data to ggplot and make a nice barchart.

like image 488
xBarns Avatar asked Jun 18 '18 06:06

xBarns


People also ask

What does Tibble :: Enframe () do?

See Also tibble() constructs a tibble from individual columns. enframe() converts a named vector to a tib- ble with a column of names and column of values.

What is the difference between a Dataframe and a Tibble?

1. Tibble displays data along with data type while displaying whereas data frame does not. 2. Tibble fetches data using data source in its original form instead of data frame such factors, characters or numeric.

Is a Tibble a data frame?

Tibbles are data. frames that are lazy and surly: they do less (i.e. they don't change variable names or types, and don't do partial matching) and complain more (e.g. when a variable does not exist). This forces you to confront problems earlier, typically leading to cleaner, more expressive code.


1 Answers

Try

as.data.frame(F4_Gesamt_sum)

or

(function(x)data.frame(Type=names(x), Value=x))(F4_gesamt_sum)
like image 193
lebatsnok Avatar answered Oct 02 '22 19:10

lebatsnok