0

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.

  • `my_tibble %>% as.data.frame()` – TarJae Jul 23 '22 at 07:07
  • @TarJae I got the following message when I tried above code: Error in my_tibble(.) : could not find function "my_tibble". – Joseph Carthof Jul 23 '22 at 07:11
  • It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. As is one can only guess what's the issue, e.g. running `library(tibble); mtcars_tbl <- as_tibble(mtcars); mtcars_df <- as.data.frame(mtcars_tbl); print(class(mtcars_tbl)); class(mtcars_df)` shows that `as.data.frame` should give you the desired result. – stefan Jul 23 '22 at 07:12
  • 1
    Also, you say: "can use the preferred columns I have in my tibble to create a new tibble." Not sure what you exactly mean by that. But there is probably no reason to first convert to a data.frame. – stefan Jul 23 '22 at 07:15
  • I want do do a small calculation (divide a new column with my two newly created columns). When I try to calculate I get an error and I guess it has to with the df I'm referring to is old and don't contain the columns I've created. @stefan – Joseph Carthof Jul 23 '22 at 07:20
  • 1
    Hi Joseph. Have a look at the link I referenced. Without a MRE we can't help you, i.e. create a small snippet of your dataset `MacBook`. Edit your post and add this snippet and the code which gives rise to the error to your post (not as comment). – stefan Jul 23 '22 at 07:32
  • @stefan I've updated my post now with desired info. You can disregard my earlier code I wrote. – Joseph Carthof Jul 23 '22 at 07:48
  • 1
    @stefan It started with a transformation from tibble to dataframe and ended up somewhere. But now I think we could provided adequate help. Hurray! – TarJae Jul 23 '22 at 08:15

1 Answers1

1

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()
TarJae
  • 72,363
  • 6
  • 19
  • 66