r - 根据 R 中的偶数/奇数行分配值

标签 r indexing rows assign

我有一个包含 67000 行的数据框,我想将奇数行的内容替换为 Al偶数行 Bt 。它应该很简单,但我遗漏了一些东西。

 dataframe$variable <- "NA"`

 odd_indexes<-seq(1,nrow(dataframe),2)

 even_indexes<-seq(2,nrow(dataframe),2)

以下代码不起作用。

 dataframe$variable[odd_indexes,] <- "Al"

 dataframe$variable[even_indexes,] <- "Bt"

我收到此错误:Error in dataframe$variable[odd_indexes, ] : incorrect number of dimensions

最佳答案

rep() 可以解决这个问题:

dataframe$variable <- rep(c("Al", "Bt"), length.out=nrow(dataframe))

关于r - 根据 R 中的偶数/奇数行分配值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48704980/

相关文章:

iOS UITableView 自动调整单元格大小并带有部分索引

在行和列中输出数字的 C++ 代码

vba - Excel - 根据单元格值删除当前行和上面的行

sql - 为了创建高质量的 Django 应用程序,我需要了解哪些关于数据库的知识?

正则表达式 - 在逗号上拆分字符串,跳过平衡括号之间的任何内容

r - 无法加载 R DESeq2 库,安装了所有缺失的软件包,但仍然存在问题

r - 我如何在R中grep?

c# - C#二维数组"Wrong number of indices inside [];expected 2"错误

mysql - 逗号将值分隔为mysql中的行

r - 在 flexdashboard 中结合 Storyboard和 .no-mobile?