Anisha
March 24, 2020, 2:40am
1
I'm still fairly new to tibbles and had a question about it.
I was mutating a data frame to include new columns, and it suddenly turned into a tibble. Is there a reason why this happened?
I have the code below - the combined_dataframe was a regular data frame, but the resulting tf_idf ended up being a tibble:
tf_idf <- combined_dataframe %>%
add_count(word) %>%
group_by(source) %>%
mutate("IDF" = 1 + log(3 / n)) %>%
mutate("TF_IDF" = TF * IDF)
Sharp eyes. This is simply a consequence of the design decisions for the dplyr::group_by()
function.
suppressPackageStartupMessages(library(dplyr))
class(mtcars)
#> [1] "data.frame"
mtcars %>% group_by(mpg) -> obj
class(obj)
#> [1] "grouped_df" "tbl_df" "tbl" "data.frame"
obj
#> # A tibble: 32 x 11
#> # Groups: mpg [25]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # … with 22 more rows
Created on 2020-03-23 by the reprex package (v0.3.0)
2 Likes
Anisha
March 24, 2020, 1:18pm
3
@technocrat thank you for clearing that up for me!
2 Likes
system
Closed
March 31, 2020, 1:18pm
4
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.