Hello guys I have a list a dataframe as you see below
listA <- list("Jon", "Maria", "Jon", "Maria", "Ben")
Name <- c("Jon", "Bill", "Tina", "Jon", "Jon")
Age <- c(23, 41, 32, 22, 44)
df <- data.frame(Name, Age)
So what i am trying to achieve is to create an if function that will print
if (listA[1] == df$Name)
print(new_df) #under the condition
##for example a new df with all the Jons and their ages
The error I get for something like this is the following because there are more Jons than 1
Error in if (...) print(...) :
the condition has length > 1
I understand how this works for numerical values but I am struggling with the strings. My desired output would be for example a new dataframe that will print the following values for example
Name2 <- c("Jon", "Jon", "Jon")
Age2 <- c(23, 22, 44)
new_df <- data.frame(Name2, Age2)
If you understand my question could you please provide me with your help?
list
when a vector will do. In your code it seems likelistA <- list("Jon", "Maria", "Jon", "Maria", "Ben")
would be better asvecA <- c("Jon", "Maria", "Jon", "Maria", "Ben")
- Gregor Thomas