0

The current data frame has three columns and 136 rows. I want to create a new column with first 68 rows have a fixed value="03" and next 68 rows have a fixed value='04".

select(code, year, value) ->aa1. I tried this code aa1 [ , 'month']= c(rep( '03','1:68'))

aa1 [ , 'month']= c(rep( '04', '69:136'))

Anjaly
  • 5
  • 4

2 Answers2

0

you can use rep

x$newvar <- c( rep( "03", 68) , rep( "04", 68) )

Or index

x$newvar2 <- NA
x[ 1:68  , "newvar2" ] <- "03"
x[ 69:136  , "newvar2" ] <- "04"
MatthewR
  • 2,660
  • 5
  • 26
  • 37
0

Looks like you need something like this? You need to use rep twice in this case:

aa1[ , 'month']= c(rep('03', 68), rep('04', 68))
kintany
  • 531
  • 1
  • 6
  • 15