I have a dataframe called df that looks something like the example below, but much larger. It will seem like there are lots of unnecessary data in the table, but that information is relevant to other parts of the script that I'm not having trouble with. It is specifically this section I'm struggling with.
| client | metric | filter_value |
|---|---|---|
| Client A | Location | Chicago |
| Client A | Industry | Manufacturing |
| Client A | Is_Top_Earner | 0 |
| Client B | Location | New York |
| Client B | Industry | Healthcare |
| Client B | Is_Top_Earner | 0 |
| Client C | Location | Boston |
| Client C | Industry | Finance |
| Client C | Is_Top_Earner | 1 |
| Client C | Location | New York |
| Client C | Location | Philadelphia |
I want to check to see if clients have multiple locations, and if so, run a different if statement. I'm going to be aggressively simplifying what I'm trying to do by including a print statement, as it's really just the contents of the if statement that I'm struggling with. Here is what I've tried:
I started by creating the cnt dataframe, which checks the number of locations per client
filter(metric == "Location") %>%
select(client, filter_value) %>%
distinct() %>%
group_by(client) %>%
tally() %>%
filter(n > 1)
I then used the cnt table in the following if statement:
for(client in df){
if(df$client == n2$client && n2$n != 2){
paste(df$client," has more than 2 locations")
}
else{ paste(df$client," has only one location")}
}
However, I'm getting an error because it's trying to compare the entire column client in each dataframe, not the individual values in the column. I know I could use a bracket to pick out an individual client, but I want to go through the full list of them
Aucun commentaire:
Enregistrer un commentaire