I have data from my Facebook, Twitter, Instagram, Youtube, and LinkedIn accounts that I'd like to analyze. I have a data frame similar to the following:
df <- data.frame(tw_likes = c(5,4,6,NA,NA,NA,NA,NA,NA),
tw_comments = c(3,5,NA,NA,NA,NA,NA,NA,NA),
fb_likes = c(NA,NA,NA,7,4,8,NA,NA,NA),
fb_comments = c(NA,NA,NA,NA,NA,7,NA,NA,NA),
ig_likes = c(NA,NA,NA,NA,NA,NA,NA,NA,5),
ig_comments = c(NA,NA,NA,NA,NA,NA,43,4,2))
what I want to do is create an additional column Platform
that will take the values of "Twitter, "Facebook, or "Instagram" based on the above dataframe.
My tactic has been the following:
for(i in 1:nrow(df){
if(!is.na(df$tw_likes[i]) | !is.na(df$tw_comments[i])){
df$Platform[i] <- "Twitter"
}
else if(!is.na(df$fb_likes[i]) | !is.na(df$fb_comments[i])){
df$Platform[i] <- "Facebook"
}
else if(!is.na(df$ig_likes[i]) | !is.na(df$ig_comments[i])){
df$Platform[i] <- "Instagram"
}
}
This does work, but becomes messier to read. In reality I have more columns and more social media platforms to deal with, so is there a way to pipe the data so I at least don't have to write df$
so many times?
Another thought I had was if I couldn't remove the df$
s, could I combine the !is.na()
statements to be one statement per if statement?