I'm doing an R exercise on data extraction from a data frame. The data is as follows:
team_name <- c("Bulls", "Warriors")
wins <- c(72, 73)
losses <- c(10, 9)
is_champion <- c(TRUE, FALSE)
season <- c("1995-96", "2015-16")
great_nba_teams <- data.frame(team_name, wins, losses, is_champion, season)
There is no problem in extracting a row and I understand the need of a comma after the vector name in the code:
filter <- great_nba_teams$is_champion == TRUE
great_nba_teams[filter,]
team_name wins losses is_champion season
1 Bulls 72 10 TRUE 1995-96
However, when I tried not using a comma, I can't extract the is_champion
column. Instead, other columns are returned.
> great_nba_teams[filter]
team_name losses season
1 Bulls 10 1995-96
2 Warriors 9 2015-16
That is the same as great_nba_teams[,filter]
. Can I know what it means by [filter]
and why it is the same as [,filter]
? And why the code does not return the data of is_champion
?
Thank you very much.