2

我正在尝试使用以下代码复制站号 24 次:

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"

但是,我收到警告说:

> 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

谁能解释我在这里做错了什么?谢谢。

预期的:

每个站从站 1 到站 24 各 24 次。

4

2 回答 2

3

这是你想要的吗?

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

或者,如果您希望具有相同编号的电台一起出现:

rep(x = paste0("station", 1:24), each = 24)
于 2013-09-03T19:56:53.610 回答
1

这将创建一个 24 元素列表,其中每个元素都是一个 24 长度的向量:

stnid <- list()

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

 stnid
于 2013-09-03T22:49:39.193 回答