循环在循环中创建R中的向量

时间:2016-09-15 11:08:07

标签: r loops vector

我想用名字和数字创建一个矢量。

我有两组:A和B. 我有两个性别:男性和女性 我有2只动物:狗和猫

而不是编写包含每个组名称的向量:

vector= c("Amaledog","Afemaledog","Amalecat","Afemalecat","Bmaledog","Bfemaledog","Bmalecat","Bfemalecat")

我想在循环中使用循环:

group=c("A","B")
gender=c("female","male")
animal=c("dog","cat")

for (a in group){
 for (b in gender){
  for (c in animal){
   vector=paste0(a,b,c)
  }
 }
}

但我只获得: “Bmalecat”

你知道为什么吗?我该如何解决这个问题?

3 个答案:

答案 0 :(得分:4)

您可以使用expand.grid

expand.grid(group=c("A","B"),gender=c("female","male"),animal=c("dog","cat"))

输出:

  group gender animal
1     A female    dog
2     B female    dog
3     A   male    dog
4     B   male    dog
5     A female    cat
6     B female    cat
7     A   male    cat
8     B   male    cat

编辑:

do.call(paste0, expand.grid(group=c("A","B"),gender=c("female","male"),animal=c("dog","cat")))

正如评论中所指出的,这应该这样做。

答案 1 :(得分:1)

每次都覆盖vector,这就是问题所在。您可能想要做的是

vect <- character(length(group) * length(gender) * length(animal))

i <- 1
for (a in group){
  for (b in gender){
    for (c in animal){
      vect[i] <- paste0(a,b,c)
      i <- i + 1
    }
  }
}

预先分配向量以避免大量(低效)重新分配非常重要。您最好使用矢量化解决方案。

答案 2 :(得分:0)

以下是使用CJ和交叉加入(library(data.table) CJ(group, gender, animal)[, do.call(paste0, .SD)] #[1] "Afemalecat" "Afemaledog" "Amalecat" "Amaledog" "Bfemalecat" #[6] "Bfemaledog" "Bmalecat" "Bmaledog"

的选项
[16:19:54] Using gulpfile /var/www/html/new-webclass/gulpfile.js
[16:19:54] Starting 'build'...
[16:19:54] 'build' errored after 36 ms
[16:19:54] TypeError: gulp.src(...).pipe(...).pipe is not a function
    at Gulp.<anonymous> (/var/www/html/new-webclass/gulpfile.js:80:6)
    at module.exports (/var/www/html/new-webclass/node_modules/orchestrator/lib/runTask.js:34:7)
    at Gulp.Orchestrator._runTask (/var/www/html/new-webclass/node_modules/orchestrator/index.js:273:3)
    at Gulp.Orchestrator._runStep (/var/www/html/new-webclass/node_modules/orchestrator/index.js:214:10)
    at Gulp.Orchestrator.start (/var/www/html/new-webclass/node_modules/orchestrator/index.js:134:8)
    at /usr/lib/node_modules/gulp-cli/lib/versioned/^3.7.0/index.js:46:20
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)
    at Module.runMain (module.js:592:11)
    at run (bootstrap_node.js:394:7)
/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:623
    var written = dest.write(chunk);
                       ^

TypeError: dest.write is not a function
    at write (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:623:24)
    at flow (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:632:7)
    at DestroyableTransform.pipeOnReadable (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:664:5)
    at emitNone (events.js:86:13)
    at DestroyableTransform.emit (events.js:185:7)
    at emitReadable_ (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:448:10)
    at emitReadable (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:444:5)
    at readableAddChunk (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:187:9)
    at DestroyableTransform.Readable.push (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:149:10)
    at DestroyableTransform.Transform.push (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_transform.js:145:32)