簡體   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