I am doing Exploratory Data Analytics of Sports Data using R Programming... I want to print the records from the Data Frame based on two condition condition: Country == "England" ||(OR operator) Ground == ground can be any one from the below list
List = c("Birmingham" ,"Bristol" ,"Cardiff" ,"Chester-le-Street" ,
"Leeds" ,"East London" ,"Lord's", "The Oval",
"Manchester" ,"Nottingham" ,"Southampton" ,"Taunton")
Sample Data (abbreviated given image provided)
data.frame(
Ground = c('Hambantota', 'Benoni', 'Benoni', 'Hambantota', 'Hambantota',
'Pallekele', 'Pallekele'),
Country = c('Bangladesh', 'SouthAfrica', 'Pakistan', 'SriLanka', 'Bangladesh',
'SriLanka', 'Bangladesh')
)
I am trying...
subset(WC_Grounds,WC_Grounds$Country=="England"||WC_Grounds %in% WC_Grounds_List)
but it returns the 0 records
Does
subset(WC_Grounds, WC_Grounds$Country=="England" | WC_Grounds$Ground %in% WC_Grounds_List)
Work for you?
|| and && - These operators are “short-circuiting”: as soon as || sees the first TRUE it returns TRUE without computing anything else. You should instead use
|
which is vectorized thus applying to the multiple values in your dataset.
Here is an example using the abbreviated sample data I added to your question:
WC_Grounds <- data.frame(
Ground = c('Hambantota', 'Benoni', 'Benoni', 'Hambantota', 'Hambantota',
'Pallekele', 'Pallekele'),
Country = c('Bangladesh', 'SouthAfrica', 'Pakistan', 'SriLanka', 'Bangladesh',
'SriLanka', 'Bangladesh')
)
List = c('Hambantota', 'Benoni')
subset(WC_Grounds, WC_Grounds$Country == "SriLanka" | WC_Grounds$Ground %in% List)
#> Ground Country
#> 1 Hambantota Bangladesh
#> 2 Benoni SouthAfrica
#> 3 Benoni Pakistan
#> 4 Hambantota SriLanka
#> 5 Hambantota Bangladesh
#> 6 Pallekele SriLanka
Created on 2021-03-20 by the reprex package (v1.0.0)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.