I have a dataframe (df) with a column of type strings such as
code country
DK Denmark
DE Germany
BEB Belgium
B Brazil
And I wont to write a for loop that prints the countries starting with B. This is how I approach it but I get an error:
for (i in unique(df$country)) {
if (i == grep("^B", unique(df$country), value = TRUE)) {
print(i)
}
}
Error:
Error in if (i == grep("^B", unique(df$country), value = TRUE)) { :
the condition has length > 1
CodePudding user response:
We don't need a loop here as grep
is vectorized
unique(subset(df, grepl("^B", country), select = country))
-output
country
3 Belgium
4 Brazil
Regarding the issue in the loop, it is just that ==
does elementwise comparison and returns the logical vector with the length of the second vector as the first one is just of length 1.
'Belgium' == grep("^B", unique(df$country), value = TRUE)
[1] TRUE FALSE
and this triggers the length warning as if/else
expects a vector of length 1 as input
If we use %in%
, it returns a single TRUE/FALSE
for (i in unique(df$country)) {
if (i %in% grep("^B", unique(df$country), value = TRUE)) {
print(i)
}
}
-output
[1] "Belgium"
[1] "Brazil"
data
df <- structure(list(code = c("DK", "DE", "BEB", "B"), country = c("Denmark",
"Germany", "Belgium", "Brazil")), class = "data.frame",
row.names = c(NA,
-4L))