繁体   English   中英

从R中的数据帧中提取

[英]extract from a data frame in R

这是我的主要数据:

1    Cl1  28    2750 2015-05-16 Marseille        S7
2    Cl1  27    2570 2015-06-03 Marseille        S7
3 Cl1000  24    1950 2015-07-05 Marseille       S17
4 Cl1000  17    1400 2016-01-09 Marseille       S17
5  Cl104  29    2680 2015-01-02  Grenoble        S3
6  Cl110  29    2660 2016-02-02    Calais        S2

我想提取2015年每个月的Qte数量以及类型S1和S2。

df<-sqldf("select Qte, typologie  from 
          where ='S1, S2'
          And data_achat.year = 2015'
          Group by date_achat.month  
           ")  

结果是Null。

我还想可视化和比较每月的数量,应该使用哪个图?

假设所需的是那些年份为2015TypologieS1S2行的按Client和月份的Qte之和,我们在下面的注释中以可复制的形式提供了一些测试数据。

由于在问题显示的数据中没有年份是2015行,而TypologieS1S2 ,因此我们在第7行添加了该行。

请注意,年份是日期的前4个字符,月份是从日期的位置6开始的2个字符。

library(sqldf)

sqldf("select Client, substr(Date_achat, 6, 2) month, sum(Qte) Qte, typologie
       from AllInfosClients 
       where Typologie in ('S1', 'S2') and substr(Date_achat, 1, 4) = '2015'
       group by 1, 2")

赠送:

  Client month Qte Typologie
1  Cl110    02  29        S2

注意

可复制形式的输入:

Lines <- "
Client Qte Montant Date_achat     Ville Typologie
1    Cl1  28    2750 2015-05-16 Marseille        S7
2    Cl1  27    2570 2015-06-03 Marseille        S7
3 Cl1000  24    1950 2015-07-05 Marseille       S17
4 Cl1000  17    1400 2016-01-09 Marseille       S17
5  Cl104  29    2680 2015-01-02  Grenoble        S3
6  Cl110  29    2660 2016-02-02    Calais        S2
7  Cl110  29    2660 2015-02-02    Calais        S2"
AllInfosClients <- read.table(text = Lines)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM