将值 "Good" 和 "Bad" 转换为布尔值以计算百分比(即,从所有记录中,80% 是 "Good")- 使用 SQL
Convert values "Good" and "Bad" to boolean in order to calculate percentage (i.e, from all records, 80% were "Good") - Using SQL
假设我有一个示例数据集 p1,其中不同的用户对 4 种不同类型的食物进行了评分,如下所示:
Food_ID Rating
1 Good
1 Good
2 Good
2 Bad
1 Bad
3 Bad
3 Good
4 Bad
1 Bad
4 Good
4 Good
4 Good
2 Bad
3 Bad
我如何编写代码,根据好与坏的评分告诉我 Food_ID 的接受度?
SELECT
Food_ID,
sum(Rating)/count(Rating) AS Avg_Rating
FROM p1
GROUP BY Food_ID
显然这段代码不起作用,因为评级是字符列...有没有快速的方法将其转换为布尔值?那么也许它可能?或者有更简单的方法吗?
谢谢!
编辑更新:
感谢下面的回答,我取得了进步。以下代码:
sqldf("
SELECT food_id,
sum(CASE WHEN rating = 'Good' THEN 1 ELSE 0 END) AS good_reviews,
count(*) AS all_reviews,
sum(CASE WHEN rating = 'Good' THEN 1 ELSE 0 END) / count(*) AS acceptance
FROM p1
GROUP BY food_id
")
其中returns输出如下:
food_id good_reviews all_reviews acceptance
1 2 4 0
2 1 3 0
3 0 3 0
4 3 4 0
SELECT
food_id,
SUM(CASE WHEN rating = 'Good' THEN 1 ELSE 0 END) AS good_reviews,
COUNT(*) AS all_reviews,
CAST(Sum(CASE WHEN rating = 'Good' THEN 1 ELSE 0 END) AS Float)
/ CAST(COUNT(*) AS Float) * 100 AS acceptance
FROM p1
GROUP BY food_id
SUM(CASE WHEN Rating='Good' THEN 1 ELSE 0 END AS BIT)/COUNT(*) AS numericalRating
使用 IF
select
food_id,
sum(IF rating='good' then 1 else 0 end)/count(*) as avg_rating
from p1
group by food_id
最终正确答案 -- 问题源于两个整数相除并产生非整数结果。谢谢@stickybit
最终工作代码:
sqldf("
SELECT food_id,
sum(CASE WHEN rating = 'Good' THEN 1 ELSE 0 END) AS good_reviews,
count(*) AS all_reviews,
CAST(sum(CASE WHEN rating = 'Good' THEN 1 ELSE 0 END) AS Float) / CAST(count(*) AS Float)*100 AS acceptance
FROM p1
GROUP BY food_id
")
假设我有一个示例数据集 p1,其中不同的用户对 4 种不同类型的食物进行了评分,如下所示:
Food_ID Rating
1 Good
1 Good
2 Good
2 Bad
1 Bad
3 Bad
3 Good
4 Bad
1 Bad
4 Good
4 Good
4 Good
2 Bad
3 Bad
我如何编写代码,根据好与坏的评分告诉我 Food_ID 的接受度?
SELECT
Food_ID,
sum(Rating)/count(Rating) AS Avg_Rating
FROM p1
GROUP BY Food_ID
显然这段代码不起作用,因为评级是字符列...有没有快速的方法将其转换为布尔值?那么也许它可能?或者有更简单的方法吗?
谢谢!
编辑更新:
感谢下面的回答,我取得了进步。以下代码:
sqldf("
SELECT food_id,
sum(CASE WHEN rating = 'Good' THEN 1 ELSE 0 END) AS good_reviews,
count(*) AS all_reviews,
sum(CASE WHEN rating = 'Good' THEN 1 ELSE 0 END) / count(*) AS acceptance
FROM p1
GROUP BY food_id
")
其中returns输出如下:
food_id good_reviews all_reviews acceptance
1 2 4 0
2 1 3 0
3 0 3 0
4 3 4 0
SELECT
food_id,
SUM(CASE WHEN rating = 'Good' THEN 1 ELSE 0 END) AS good_reviews,
COUNT(*) AS all_reviews,
CAST(Sum(CASE WHEN rating = 'Good' THEN 1 ELSE 0 END) AS Float)
/ CAST(COUNT(*) AS Float) * 100 AS acceptance
FROM p1
GROUP BY food_id
SUM(CASE WHEN Rating='Good' THEN 1 ELSE 0 END AS BIT)/COUNT(*) AS numericalRating
使用 IF
select
food_id,
sum(IF rating='good' then 1 else 0 end)/count(*) as avg_rating
from p1
group by food_id
最终正确答案 -- 问题源于两个整数相除并产生非整数结果。谢谢@stickybit
最终工作代码:
sqldf("
SELECT food_id,
sum(CASE WHEN rating = 'Good' THEN 1 ELSE 0 END) AS good_reviews,
count(*) AS all_reviews,
CAST(sum(CASE WHEN rating = 'Good' THEN 1 ELSE 0 END) AS Float) / CAST(count(*) AS Float)*100 AS acceptance
FROM p1
GROUP BY food_id
")