Joseph Carthof
Joseph Carthof

Reputation: 25

Save tibble as a data frame?

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.

Upvotes: 0

Views: 1352

Answers (1)

TarJae
TarJae

Reputation: 79261

update after clarification:

library(tidyverse)

my_df <- df_apple %>% 
  pivot_longer(-c(Macbook, type), names_to = "Year") %>%
  pivot_wider(names_from = "type", values_from = "value") %>%
  arrange(Macbook) %>%
  separate(Macbook, c("code", "name")) %>% 
  mutate(Iphone_percentage = paste0(round(Iphone/(Iphone + Ipad),3)*100, "%"))

class(my_df)
  code  name      Year  Iphone  Ipad Iphone_percentage
  <chr> <chr>     <chr>  <int> <int> <chr>            
1 13    London    X2005     25    54 31.6%            
2 13    London    X2006     42    63 40%              
3 13    London    X2007     45    25 64.3%            
4 14    Paris     X2005     23    23 50%              
5 14    Paris     X2006     54    43 55.7%            
6 14    Paris     X2007     24    43 35.8%            
7 15    Barcelona X2005     14    23 37.8%            
8 15    Barcelona X2006     14    25 35.9%            
9 15    Barcelona X2007     42    32 56.8%            
> class(my_df)
[1] "tbl_df"     "tbl"        "data.frame"

First explanation: This not a really answer but on the way to explain things. If this does not help do the following: type dput(Macbook) in your console, then copy the output and paste it to your question via the edit link:

library(dplyr)
library(tidyr)

my_tibble_as_dataframe <- pivot_longer(Macbook, -c(Iphone, Ipad), names_to = "Year") %>% 
  pivot_wider(names_from = "Ipad", values_from = "value") %>% 
  separate(Iphone, c("code", "name")) %>% 
  as.data.frame()

# check
class(my_tibble_as_dataframe
# should be dataframe

is the same as:

library(dplyr)
library(tidyr)

my_tibble_as_dataframe <- Macbook %>% 
  pivot_longer(
    -c(Iphone, Ipad), 
    names_to = "Year") %>% 
  pivot_wider(names_from = "Ipad", values_from = "value") %>% 
  separate(Iphone, c("code", "name")) %>% 
  as.data.frame()

Upvotes: 1

Related Questions