Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在r中使用sql将两个或多个表连接成一个表_Sql_R_Join - Fatal编程技术网

如何在r中使用sql将两个或多个表连接成一个表

如何在r中使用sql将两个或多个表连接成一个表,sql,r,join,Sql,R,Join,我已经编写了以下代码,我想把这些表合并成一个大表;那么,如何使用SQL在R中实现这一点呢 user_lessthan10per <- sqldf("select count(uid) as count_of_students from adopted_user_point where points_scored between 0 and (1469*0.1)") user\u lesstha

我已经编写了以下代码,我想把这些表合并成一个大表;那么,如何使用SQL在R中实现这一点呢

user_lessthan10per  <- sqldf("select count(uid) as count_of_students
                       from adopted_user_point
                        where points_scored between 0 and (1469*0.1)")

user\u lessthan10per您可以使用条件聚合。这将返回一行三列:

select sum(case when points_scored between 0 and (1469*0.1) then 1 else 0
           end) as cnt1,
       sum(case when points_scored >(1469*0.1) and points_scored <= (1469*0.2) then 1 else 0 
           end) as cnt2,
       sum(case when points_scored >(1469*0.2) and points_scored <= (1469*0.3) then 1 else 0
           end) as cnt3
from adopted_user_point;

我会用不同的名称来命名最初的选择,二者分别命名为“u_0_10”、“u_10_20”、“u_20_30”,以明确“用户少于30%”实际上是“用户btwn20_30”,但现在它们在全球环境中是R数据帧,您真的不需要
sdldf
将它们放在一起:

user_under30per <- rbind(user_lessthan10per.
                        user_lessthan20per,
                        user_lessthan30per)
user\u低于30%
select sum(case when points_scored between 0 and (1469*0.1) then 1 else 0
           end) as cnt1,
       sum(case when points_scored >(1469*0.1) and points_scored <= (1469*0.2) then 1 else 0 
           end) as cnt2,
       sum(case when points_scored >(1469*0.2) and points_scored <= (1469*0.3) then 1 else 0
           end) as cnt3
from adopted_user_point;
select (case when points_scored between 0 and (1469*0.1) then 'Group1'
             when points_scored >(1469*0.1) and points_scored <= (1469*0.2) then 'Group2'
             when points_scored >(1469*0.2) and points_scored <= (1469*0.3) then 'Group3'
             else 'Other'
        end) as cnt3, count(*) as count_of_students
from adopted_user_point
group by (case when points_scored between 0 and (1469*0.1) then 'Group1'
               when points_scored >(1469*0.1) and points_scored <= (1469*0.2) then 'Group2'
               when points_scored >(1469*0.2) and points_scored <= (1469*0.3) then 'Group3'
               else 'Other'
          end);
user_under30per <- rbind(user_lessthan10per.
                        user_lessthan20per,
                        user_lessthan30per)
 one_and_two <- sqldf("select * from lessthan10per union all 
                                       select * from lessthan20per")
 all_three <- sqldf("select * from one_and_two union all 
                                       select * from lessthan30per")