I would like to save a tibble I've created in a chunk to a data frame, so I then can use the preferred columns I have in my tibble to create a new tibble.
I've tried my_df <- as.data.frame(my_tibble)
without success.
My dataset:
Macbook type 2005 2006 2007
13 London Iphone 25 42 45
13 London Ipad 54 63 25
14 Paris Iphone 23 54 24
14 Paris Ipad 23 43 43
15 Barcelona Iphone 14 14 42
15 Barcelona Ipad 23 25 32
#with 478 more rows
The code I've runned:
pivot_longer(df_apple, -c(Macbook, type), names_to = "Year") %>%
pivot_wider(names_from = "type", values_from = "value") %>%
separate(Macbook, c("code", "name"))
And generates the following tibble:
code name year Iphone Ipad
13 London 2005 25 54
13 London 2006 42 63
13 London 2007 45 25
#with more rows
I want to create a new column Iphone_percentage
and remove the Iphone and Ipad columns from the tibble. The column Iphone_percentage
should contain the calculation Iphone / (Iphone + Ipad)
per year.