install.packages("tidyr") to install packages
library(tidyr) to bring the package to R env
df3<-read.csv(file.choose()) to open .csv file
> df3
state X2018 X2017 X2016 X2015
1 AP 557 212 344 239
2 WB 126 551 175 492
3 KA 85 450 471 90
4 DE 378 766 736 914
5 MP 227 614 135 182
6 RJ 177 925 544 795
7 TN 489 872 607 313
8 TS 253 127 138 608
9 MA 508 418 389 641
10 HP 527 832 887 918
> names(df3)[names(df3) == 'X2018']<-2018
> names(df3)[names(df3) == 'X2017']<-2017
> names(df3)[names(df3) == 'X2016']<-2016
> names(df3)[names(df3) == 'X2015']<-2015
> df3
state 2018 2017 2016 2015
1 AP 557 212 344 239
2 WB 126 551 175 492
3 KA 85 450 471 90
4 DE 378 766 736 914
5 MP 227 614 135 182
6 RJ 177 925 544 795
7 TN 489 872 607 313
8 TS 253 127 138 608
9 MA 508 418 389 641
10 HP 527 832 887 918
dg = gather(df3,'year','n',2:5)
> dg
state year n
1 AP 2018 557
2 WB 2018 126
3 KA 2018 85
4 DE 2018 378
5 MP 2018 227
6 RJ 2018 177
7 TN 2018 489
8 TS 2018 253
9 MA 2018 508
10 HP 2018 527
11 AP 2017 212
12 WB 2017 551
13 KA 2017 450
14 DE 2017 766
15 MP 2017 614
16 RJ 2017 925
17 TN 2017 872
18 TS 2017 127
19 MA 2017 418
20 HP 2017 832
21 AP 2016 344
22 WB 2016 175
23 KA 2016 471
24 DE 2016 736
25 MP 2016 135
26 RJ 2016 544
27 TN 2016 607
28 TS 2016 138
29 MA 2016 389
30 HP 2016 887
31 AP 2015 239
32 WB 2015 492
33 KA 2015 90
34 DE 2015 914
35 MP 2015 182
36 RJ 2015 795
37 TN 2015 313
38 TS 2015 608
39 MA 2015 641
40 HP 2015 918
spread(dg,year,n)
state 2015 2016 2017 2018
1 AP 239 344 212 557
2 DE 914 736 766 378
3 HP 918 887 832 527
4 KA 90 471 450 85
5 MA 641 389 418 508
6 MP 182 135 614 227
7 RJ 795 544 925 177
8 TN 313 607 872 489
9 TS 608 138 127 253
10 WB 492 175 551 126
date<-scan(what='char')
1: 2018-06-11
2: 2019-02-10
3: 2016-05-22
4: 2015-06-12
5: 2016-11-12
6:
Read 5 items
> sno<-c(1,2,3,4,5)
> d4<-data.frame(sno,date)
> d4
sno
date
1 1 2018-06-11
2 2 2019-02-10
3 3 2016-05-22
4 4 2015-06-12
s1<-separate(d4,date,c('year','month','day'),sep='-')
> s1
sno year month day
1 1 2018 06 11
2 2 2019 02 10
3 3 2016 05 22
4 4 2015 06 12
5 5 2016 11 12
> unite(s1,'date',year,month,day,sep='/')
sno date
1 1 2018/06/11
2 2 2019/02/10
3 3 2016/05/22
4 4 2015/06/12
5 5 2016/11/12
No comments:
Post a Comment