简体   繁体   中英

Error/Warning using the replicate function in R

I am trying to replicate the station number 24 times each by using the code as follows:

stnid <- NULL

for (i in 1:24) {
  stnid[i] <- rep(paste0("station",i),times=24)
}

> stnid
 [1] "station1"  "station2"  "station3"  "station4"  "station5"  "station6"  "station7"  "station8"  "station9" 
[10] "station10" "station11" "station12" "station13" "station14" "station15" "station16" "station17" "station18"
[19] "station19" "station20" "station21" "station22" "station23" "station24"

However, I get the warnings saying :

> warnings()
Warning messages:
1: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
2: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
3: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
4: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
5: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
6: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
7: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
8: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
9: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
10: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
11: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
12: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
13: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
14: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
15: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
16: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
17: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
18: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
19: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
20: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
21: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
22: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
23: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length
24: In stnid[i] <- rep(paste0("station", i), times = 24) :
  number of items to replace is not a multiple of replacement length

Could anyone explain what did I do wrong here ? Thanks.

Expected:

each station from station 1 to station24 24 times each.

Is this what you want?

rep(x = paste0("station", 1:24), times = 24)

Or if you rather want stations with the same number to occur together:

rep(x = paste0("station", 1:24), each = 24)

This would have created a 24 element list each of whose elements was a 24 length vector:

stnid <- list()

for (i in 1:24) {
  stnid[[i]] <- rep(paste0("station",i),times=24)
}

 stnid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM