lundi 3 mai 2021

Inserting an existing value from column, to a true statement (if else) IN R


I have a df looking like this:

| Speak_Dur|CNC_count|TNT_count|...
|0.5 | 1 | 0
|0.8 | 0 | 1
|4.3 | | 1
|5.5 | 1 | 0

I want to make a few new columns using if else.
for the example, let's say I got only those 3 columns.
So I want to make a new column for each "X_count" variable I already have based on this condition:
new column "CNC_dur"= if cnc_count=1, then paste the "speak_dur" value from the same row.
new column "TNT_dur" = if tnt_count=1, then paste the "speak_dur" value from the same row.

results should be:

| Speak_Dur|CNC_count|TNT_count|CNC_dur|TNT_dur|
|0.5 | 1 | 0 |0.5 |0 |
|0.8 | 0 | 1 | 0 | 0.8
|4.3 | 0 | 1 | 0 | 4.3
|5.5 | 1 | 0 |5.5 | 0

for now, I tried:

mutate(

CNC_DUR = if_else(CNC_count[row_number() -1] =="1","speak_dur",0,0))


I guess the last line should be something else, hoping to get any help, thank you.

Aucun commentaire:

Enregistrer un commentaire