0

I want to change values of column in dataframe with condition/ couple data

Here is my sample data:

structure(list(Tahun = c(2010, 2010, 2010, 2010, 2010, 2010, 
2010, 2010, 2010, 2010, 2010, 2010, 2011, 2011, 2011, 2011, 2011, 
2011, 2011, 2011), Bulan = c("Januari", "Februari", "Maret", 
"April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", 
"November", "Desember", "Januari", "Februari", "Maret", "April", 
"Mei", "Juni", "Juli", "Agustus"), `Tgl 1` = c("X", "X", "X", 
"X", "X", "X", "-", "-", "57", "18", "X", "-", "-", "-", "-", 
"45", "8", "26", "-", "TTU"), `Tgl 2` = c("X", "X", "X", "X", 
"X", "X", "11", "-", "-", "13", "X", "8", "-", "3", "11", "-", 
"4", "-", "8", "28"), `Tgl 3` = c("X", "X", "X", "X", "X", "X", 
"8", "-", "-", "-", "X", "45", "6", "10", "-", "14", "15", "-", 
"3", "45"), `Tgl 4` = c("X", "X", "X", "X", "X", "X", "3", "73", 
"-", "10", "X", "-", "-", "TTU", "-", "2", "14", "8", "25", "6"
), `Tgl 5` = c("X", "X", "X", "X", "X", "X", "TTU", "69", "-", 
"4", "X", "-", "-", "-", "-", "TTU", "17", "34", "32", "-")), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

and I want to insert month_index like this:

   Tahun     Bulan  month_index Tgl 1 Tgl 2 Tgl 3 Tgl 4 Tgl 5
1   2010   Januari            1     X     X     X     X     X
2   2010  Februari            2     X     X     X     X     X
3   2010     Maret            3     X     X     X     X     X
4   2010     April            4     X     X     X     X     X
5   2010       Mei            5     X     X     X     X     X
6   2010      Juni            6     X     X     X     X     X
7   2010      Juli            7     -    11     8     3   TTU
8   2010   Agustus            8     -     -     -    73    69
9   2010 September            9    57     -     -     -     -
10  2010   Oktober           10    18    13     -    10     4
11  2010  November           11     X     X     X     X     X
12  2010  Desember           12     -     8    45     -     -
13  2011   Januari            1     -     -     6     -     -
14  2011  Februari            2     -     3    10   TTU     -
15  2011     Maret            3     -    11     -     -     -
16  2011     April            4    45     -    14     2   TTU
17  2011       Mei            5     8     4    15    14    17
18  2011      Juni            6    26     -     -     8    34
19  2011      Juli            7     -     8     3    25    32
20  2011   Agustus            8   TTU    28    45     6     -

is there an easier way to change values ​​on bulan than to use contoh_2$contoh_index[bulan=="Januari"]<-1

Here the values to change:

       bulan month
1    Januari     1
2   Februari     2
3      Maret     3
4      April     4
5        Mei     5
6       Juni     6
7       Juli     7
8    Agustus     8
9  September     9
10   Oktober    10
11  November    11
12  Desember    12
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ExHunter
  • 307
  • 4
  • 11

1 Answers1

0

If you already have the lookup dataframe, you can match or merge it with df :

df$month_index <- lookup$month[match(df$Bulan, lookup$bulan)]

Note that in this case you get expected output only by doing match(df$Bulan, lookup$bulan).

Also look at inbuilt constant month.name which returns months of the year.

Perhaps, using merge is easy :

merge(df, lookup, by.x = 'Bulan', by.y = 'bulan')
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213