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.
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.
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.
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.
Try
as.data.frame(F4_Gesamt_sum)
or
(function(x)data.frame(Type=names(x), Value=x))(F4_gesamt_sum)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With