Help To generate new column

I dont knnow hot to generate new column to obtain(for PACHET ex:448602 to have same value :1300 and so on .Thanks a lot
CTRL PACHET Sigla deseu TOTAL New_TOTAL
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

Would you please supply your data in dput() format? It is just about unuseable in its present state.

Just do dput(mydata) where mydata is your data. Copy the output and paste it here between
```

```

1 Like

Check out the welcome message for help on how to ask questions.

Using dplyr::mutate is one way to create new columns or variables in your data. Something like this might work.

# package libraries
library(tidyverse)

# generate 'sample' data to answer question
sample_data <- iris %>%
  as_tibble()

# use `mutate` to create new column, but do not save to data 
sample_data %>%
  mutate(
    new_column = "448602"
  )
#> # A tibble: 150 × 6
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species new_column
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>   <chr>     
#>  1          5.1         3.5          1.4         0.2 setosa  448602    
#>  2          4.9         3            1.4         0.2 setosa  448602    
#>  3          4.7         3.2          1.3         0.2 setosa  448602    
#>  4          4.6         3.1          1.5         0.2 setosa  448602    
#>  5          5           3.6          1.4         0.2 setosa  448602    
#>  6          5.4         3.9          1.7         0.4 setosa  448602    
#>  7          4.6         3.4          1.4         0.3 setosa  448602    
#>  8          5           3.4          1.5         0.2 setosa  448602    
#>  9          4.4         2.9          1.4         0.2 setosa  448602    
#> 10          4.9         3.1          1.5         0.1 setosa  448602    
#> # ℹ 140 more rows

Created on 2024-02-02 with reprex v2.0.2

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 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
library(tidyverse)

# Old data frame
(old_df <- tibble(
  CTRL = rep("CTRV1", 10),
  PACHET = c(495502, 448602, 448602, 448602, 448603, 448604, 448670, 448670, 448670, 448690),
  Sigla = c("", "", "ALD", "MFLK", "", "", "", "ALD", "MFLK", ""),
  deseu = c(0, 0, 2, 9, 0, 0, 0, 6, 4, 0),
  TOTAL = c(2497, 1300, 0, 0, 1200, 200, 2300, 0, 0, 1165)
))
 

(result <- mutate(old_df,
                 NEW_TOTAL = max(TOTAL),
          .by = PACHET) |> select(-TOTAL))

An alternative is

library(data.table)
DT <-data.table(  CTRL = c("CTRV1","CTRV1","CTRV1",
                        "CTRV1","CTRV1","CTRV1","CTRV1","CTRV1","CTRV1",
                        "CTRV1"),
             PACHET = c(495502L,448602L,448602L,
                        448602L,448603L,448604L,448670L,448670L,448670L,
                        448690L),
       Sigla = c(NA, NA, "ALD", "MFLK", NA, NA, NA, "ALD", "MFLK", NA),
       deseu = c(0L, 0L, 2L, 9L, 0L, 0L, 0L, 6L, 4L, 0L),
       TOTAL = c(2497L, 1300L, 0L, 0L, 1200L, 200L, 2300L, 0L, 0L, 1165L)
       )
DT[, NEWTOTAL := max(TOTAL), by = "PACHET"]
	#Or more tidily, assuming we don't need the old TOTAL
DT [TOTAL:= max(TOTAL), by = "PACHET"]

CTRL PACHET Sigla deseu NEW_TOTAL by

1 CTRV 495502 "" 0 2497 495502
2 CTV1 448602 "" 0 1300 448602
3 CTV1 448602 "ALD" 2 1300 448602
4 CTV1 448602 "MFLK" 9 1300 448602
5 CTV1 448603 "" 0 1200 448603
6 CTV1 448604 "" 0 200 448604
7 CTV1 448670 "" 0 2300 448670
8 CTV1 448670 "ALD" 6 2300 448670
9 CTV1 448670 "MFLK" 4 2300 448670
10 CTV1 448690 "" 0 1165 44869

PACHET are doubled by :"by "

Thakns a lot anyway

I dont understand what you mean, could you use more words to describe you issue, if you have one ?
if you wrote by instead of .by that would be a typographical error.

I run the code I have two column with the same value:PACHET and the last one.thanks

I dont know what you are trying to say.
sorry

Sorry for my english.please run code .yoy will see .Please close the topic

Are we closing the topic because the issues are solved? or because we are giving up proceeding with it ?

please run code .yoy will see

If I run the code I shared with you on the example data you had given the result is.

# A tibble: 10 × 5
   CTRL  PACHET Sigla  deseu NEW_TOTAL
   <chr>  <dbl> <chr>  <dbl>     <dbl>
 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 topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.