How can I add new column

How can I add new column (Nex _TOTAL) :
CTRL PACHET Sigla deseu TOTAL New_TOTAL.Each value should be the same if PACHET are the same (448602
should corespondent in New_TOTAL ,value 1300
1 CTRV 495502 "" 0 2497 2497
2 CTV1 448602 "" 0 1300 1300
3 CTV1 448602 "ALD" 2 0 1300
4 CTV1 448602 "MFLK" 9 0 1300
5 CTV1 448603 "" 0 1200 1200
6 CTV1 448604 "" 0 200 200
7 CTV1 448670 "" 0 2300 2300
8 CTV1 448670 "ALD" 6 0 2300
9 CTV1 448670 "MFLK" 4 0 2300
10 CTV1 448690 "" 0 1165 1165

Try base::ifelse() R ifelse() Usage with Examples - Spark By {Examples} (sparkbyexamples.com) or
dplyr::if_else() Vectorised if-else — if_else • dplyr (tidyverse.org)

Also, please make your example reproducible next time with the code you tried.

1 Like

I tried to use mutate but it is dificult becauase i have to copy from difernet row :
Old data frame :

CTRL PACHET Sigla deseu TOTAL
<chr
1 CTRV1 495502 0 2497
2 CTRV1 448602 0 1300
3 CTRV1 448602 ALD 2 0
4 CTRV1 448602 MFLK 9 0
5 CTRV1 448603 0 1200
6 CTRV1 448604 0 200
7 CTRV1 448670 0 2300
8 CTRV1 448670 ALD 6 0
9 CTRV1 448670 MFLK 4 0
10 CTRV1 448690 0 1165
New data frame
CTRL PACHET Sigla deseu TOTAL
--- --- --- --- --- ---
<chr
1 CTRV1 495502 0 2497
2 CTRV1 448602 0 1300
3 CTRV1 448602 ALD 2 0
4 CTRV1 448602 MFLK 9 0
5 CTRV1 448603 0 1200
6 CTRV1 448604 0 200
7 CTRV1 448670 0 2300
8 CTRV1 448670 ALD 6 0
9 CTRV1 448670 MFLK 4 0
10 CTRV1 448690 0 1165

Sorry new date frame :

CTRL PACHET Sigla deseu NEW_TOTAL
<chr
1 CTRV1 495502 0 2497
2 CTRV1 448602 0 1300
3 CTRV1 448602 ALD 2 1300
4 CTRV1 448602 MFLK 9 1300
5 CTRV1 448603 0 1200
6 CTRV1 448604 0 200
7 CTRV1 448670 0 2300
8 CTRV1 448670 ALD 6 2300
9 CTRV1 448670 MFLK 4 2300
10 CTRV1 448690 0 1165

this thread is closed in favour of Help To generate new column

2 Likes