Reputation: 422
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?
Upvotes: 2
Views: 997
Reputation: 66425
Here's another approach using dplyr
and tidyr
to pull the data into long format, filter out the blanks, and add the longer name based on a lookup table:
library(tidyr); library(dplyr)
df %>%
pivot_longer(cols = everything(),
names_to = c("pltfm", "stat"),
names_sep = "_",
values_to = "value") %>%
filter(!is.na(value)) %>%
left_join(
tibble(pltfm = c("tw", "fb", "ig"),
Platform = c("Twitter", "Facebook", "Instagram"))
)
#Joining, by = "pltfm"
## A tibble: 13 x 4
# pltfm stat value Platform
# <chr> <chr> <dbl> <chr>
# 1 tw likes 5 Twitter
# 2 tw comments 3 Twitter
# 3 tw likes 4 Twitter
# 4 tw comments 5 Twitter
# 5 tw likes 6 Twitter
# 6 fb likes 7 Facebook
# 7 fb likes 4 Facebook
# 8 fb likes 8 Facebook
# 9 fb comments 7 Facebook
#10 ig comments 43 Instagram
#11 ig comments 4 Instagram
#12 ig likes 5 Instagram
#13 ig comments 2 Instagram
Upvotes: 2
Reputation: 1784
Here's an option with dplyr
's case_when()
df %>%
mutate(Plataform = case_when(
!is.na(tw_likes) | !is.na(tw_comments) ~ "Twitter",
!is.na(fb_likes) | !is.na(fb_comments) ~ "Facebook",
!is.na(ig_likes) | !is.na(ig_comments) ~ "Instagram"))
Upvotes: 4
Reputation: 887048
Here is one way in base R
to split the dataset into a list
of same prefix columns (by removing the suffix substring from the column names), do a rowSums
to create a logical matrix
, apply max.col
to get the column position for each row and change that index by passing a vector of replacement values in the same order of split column names
i1 <- max.col(sapply(split.default(df, sub("_.*", "", names(df))),
function(x) rowSums(!is.na(x)) > 0 ), 'first')
df$Platform <- c("Facebook", "Instagram", "Twitter")[i1]
df$Platform
#[1] "Twitter" "Twitter" "Twitter" "Facebook" "Facebook"
#[6] "Facebook" "Instagram" "Instagram" "Instagram"
Upvotes: 4