please can anyone help me to find a code to add the word FRANKFURT to a list of customer numbers

I am a beginner in R. Anysimple code to join the word FRANKFURT to a column full of customer numbers.

Hi @arivz,
Welcome to the RStudio Community Forum.

This is a very trivial task in R (and one which you could easily have answered yourself by reading one of the many, free, on-line resources on learning R):

df <- data.frame(Customer = c(78,123,45,22,11),
                 Sales = c(1245, 1833, 776, 545, 1108))
df
#>   Customer Sales
#> 1       78  1245
#> 2      123  1833
#> 3       45   776
#> 4       22   545
#> 5       11  1108

df$New_column <- paste("FRANKFURT", df$Customer, sep="_")
df
#>   Customer Sales    New_column
#> 1       78  1245  FRANKFURT_78
#> 2      123  1833 FRANKFURT_123
#> 3       45   776  FRANKFURT_45
#> 4       22   545  FRANKFURT_22
#> 5       11  1108  FRANKFURT_11

Created on 2021-06-05 by the reprex package (v2.0.0)

Thanks a lot Davo, it worked. Still trying to under stand the basics. Wonder if the same can be done using iteration.
There is a column full of customer numbers from 1000 to 1500 in order and I have to add the word frankfurt.

I tried using i
for (i in seq(1000, 1500 )) {
airport$customer_number <- paste0("FRANKFURT, i , sep"")
}

I am able to add the word FRANKFURT to the customer number but all the rows had FRANKFURT 1500 , FRANKFURT1500, FRANKFURT 1500 ......for 500 entries while I was expecting FRANKFURT 1000 , FRANKFURT 1001, FRANKFURT1002......FRANKFURT1500)

I wasnt sure why it was not able to keep the sequence of 1000 to 1500 but picked only the last customer number

Can you suggest where i m going wrong

Many operations in R are vectorized. That means that the action is automatically performed on every element of a vector or every row of a data frame without the need for explicitly iterating with a loop. Notice in @DavoWW 's answer that he added FRANKFURT to every row with no loop.

df$New_column <- paste("FRANKFURT", df$Customer, sep="_")

Your loop makes every row FRANKFURT1000, then overwrites that with FRANKFURT1001 and keeps overwriting until the last iteration of FRANKFURT1500. You can use

airport$customer_number <- paste0("FRANKFURT, airport$customer , sep"")

without a loop

Thanks for explaining, much appreciate

This topic was automatically closed 7 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.