Select 行数据框基于 R 编程中列出的值
Select rows from Data Frame based on listed values in R Programming
我正在使用 R 编程对体育数据进行探索性数据分析...
我想根据两个条件从数据框中打印记录
条件:国家==“英格兰”||(或运算符)地面==地面可以是以下列表中的任何一个
List = c("Birmingham" ,"Bristol" ,"Cardiff" ,"Chester-le-Street" ,
"Leeds" ,"East London" ,"Lord's", "The Oval",
"Manchester" ,"Nottingham" ,"Southampton" ,"Taunton")
示例数据(提供的缩写图像)
data.frame(
Ground = c('Hambantota', 'Benoni', 'Benoni', 'Hambantota', 'Hambantota',
'Pallekele', 'Pallekele'),
Country = c('Bangladesh', 'SouthAfrica', 'Pakistan', 'SriLanka', 'Bangladesh',
'SriLanka', 'Bangladesh')
)
我正在尝试...
subset(WC_Grounds,WC_Grounds$Country=="England"||WC_Grounds %in% WC_Grounds_List)
但是它 returns 0 条记录
有
subset(WC_Grounds, WC_Grounds$Country=="England" | WC_Grounds$Ground %in% WC_Grounds_List)
为你工作?
|| 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.
这是一个使用我添加到您的问题中的简化示例数据的示例:
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
由 reprex package (v1.0.0)
于 2021 年 3 月 20 日创建
我正在使用 R 编程对体育数据进行探索性数据分析... 我想根据两个条件从数据框中打印记录 条件:国家==“英格兰”||(或运算符)地面==地面可以是以下列表中的任何一个
List = c("Birmingham" ,"Bristol" ,"Cardiff" ,"Chester-le-Street" ,
"Leeds" ,"East London" ,"Lord's", "The Oval",
"Manchester" ,"Nottingham" ,"Southampton" ,"Taunton")
示例数据(提供的缩写图像)
data.frame(
Ground = c('Hambantota', 'Benoni', 'Benoni', 'Hambantota', 'Hambantota',
'Pallekele', 'Pallekele'),
Country = c('Bangladesh', 'SouthAfrica', 'Pakistan', 'SriLanka', 'Bangladesh',
'SriLanka', 'Bangladesh')
)
我正在尝试...
subset(WC_Grounds,WC_Grounds$Country=="England"||WC_Grounds %in% WC_Grounds_List)
但是它 returns 0 条记录
有
subset(WC_Grounds, WC_Grounds$Country=="England" | WC_Grounds$Ground %in% WC_Grounds_List)
为你工作?
|| 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.
这是一个使用我添加到您的问题中的简化示例数据的示例:
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
由 reprex package (v1.0.0)
于 2021 年 3 月 20 日创建