使用R中的复制功能进行错误/警告

时间:2013-09-03 19:47:21

标签: r

我试图通过使用以下代码将每个站号复制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次。

2 个答案:

答案 0 :(得分:3)

这是你想要的吗?

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

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

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

答案 1 :(得分:1)

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

stnid <- list()

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

 stnid