Reputation: 3
i need to create a new column that is a copy of the already existing 'Engagement' column that I have. That new column (named Hello) needs to be ordered in two categories divided by the mean (8 values above the mean and in the other category the 8 values under the mean). I tried using the functions mutate and arrange of the dplyr package, but nothing has worked so far. I get the error code : no applicable method for 'mutate' applied to an object of class "c('double', 'numeric')" when i try the code :
mutate (classe_df, Hello, Engagement)
My code has 16 values, where the engagement column is made of the likes divided by the views.
Here is my complete code :
Sujet <- sprintf("s_%d", 1:16)
Titre <- c("Coffee cake cookies", "Poutine baloney", "Polite Boy","Infinity water glitch", "Garlic butter steak bites", "Sun Conure", "Pizza in my room", "Parkour skills", "Supercars", "Brown monster", "Batch of pasta", "Police grappler", "F340", "Ceramic brakes", "GT3 Seat Solution", "Over Priced GT2RS" )
Likes <- c(150700, 11200, 3500000, 256700, 1400000, 791300, 439900, 382700, 45400, 1300000, 246200, 32200, 319000, 1892, 3413, 17400)
Vues <- c(1300000, 214500, 13500000, 11400000, 17100000, 6700000, 400000, 11300000, 203100, 18400000, 3000000, 1000000, 1800000, 12600, 23500, 213800)
Engagement <- c(150700 / 1300000, 11200 / 214500, 3500000 / 13500000, 256700 / 11400000, 1400000 / 17100000, 791300 / 6700000, 439900 / 4000000, 382700 / 11300000, 45400 / 203100, 1300000 / 18400000, 246200 / 3000000, 32200 / 1000000, 319000 / 1800000, 1892 / 12600, 3413 / 23500, 17400 / 213800)
classe_df <- data.frame(Sujet, Titre, Likes, Vues, Engagement)
#install.packages("ggplot2")
library(ggplot2)
ggplot(classe_df, aes(x = Vues, y = Likes) ) +
geom_point() +
labs(x = 'Vues', y = 'Likes', title = 'Relation entre les likes et les vues') +
theme_light()
ggsave("ggplot.jpg",
scale = 1,
width = 10,
height = 7,
units = "cm",
dpi = 300)
#install.packages("dplyr")
library(dplyr)
Upvotes: 0
Views: 78
Reputation: 437
Is this what you're after, @Sam?
Updated In Light of Sam's Edit
Sujet <- sprintf("s_%d", 1:16)
Titre <- c("Coffee cake cookies", "Poutine baloney", "Polite Boy","Infinity water glitch", "Garlic butter steak bites", "Sun Conure", "Pizza in my room", "Parkour skills", "Supercars", "Brown monster", "Batch of pasta", "Police grappler", "F340", "Ceramic brakes", "GT3 Seat Solution", "Over Priced GT2RS" )
Likes <- c(150700, 11200, 3500000, 256700, 1400000, 791300, 439900, 382700, 45400, 1300000, 246200, 32200, 319000, 1892, 3413, 17400)
Vues <- c(1300000, 214500, 13500000, 11400000, 17100000, 6700000, 400000, 11300000, 203100, 18400000, 3000000, 1000000, 1800000, 12600, 23500, 213800)
Engagement <- c(150700 / 1300000, 11200 / 214500, 3500000 / 13500000, 256700 / 11400000, 1400000 / 17100000, 791300 / 6700000, 439900 / 4000000, 382700 / 11300000, 45400 / 203100, 1300000 / 18400000, 246200 / 3000000, 32200 / 1000000, 319000 / 1800000, 1892 / 12600, 3413 / 23500, 17400 / 213800)
classe_df <- data.frame(Sujet, Titre, Likes, Vues, Engagement)
library(dplyr)
classe_df <- classe_df %>%
mutate(Hello = ifelse(median(Engagement) > Engagement, "below", "above"))
Using dplyr
I created a column that looks into classe_df$Engagement
, determines the median, and returns either "above" or "below" depending on the value's relation to the median value (half are, of course, above the median while the rest are below). If this answers your question, please kindly mark this reply correct/accepted. If it doesn't, please let me know. Thanks!
Upvotes: 0