简体   繁体   中英

extract from a data frame in R

This is my principal data :

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

I want to extract the quantities Qte per month, of the year 2015 and Typologies S1 and S2.

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

I get Null as a result.

I want also to visualise and compare quantities per month, which plot should I use?

Assuming that what is wanted is the sum of Qte by Client and month for those rows for which the year is 2015 and the Typologie is S1 or S2 we provide some test data in the Note below that is in reproducible form.

Since there was no row for which year is 2015 and Typologie is S1 or S2 in the data shown in the question we added a 7th row for which this holds.

Note that the year is the first 4 characters of the date and the month is the 2 characters starting at position 6 of the date.

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")

giving:

  Client month Qte Typologie
1  Cl110    02  29        S2

Note

The input in reproducible form:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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