简体   繁体   English

对于一个列值的每增加一个单位,另一列条目会增加

[英]For every unit increase in one column value , another column entries increase

I have a simulation dataset with 500 replicates - each replicate contains 300 ids.我有一个包含 500 个副本的模拟数据集 - 每个副本包含 300 个 ID。 When rep = 1, id ranges from 1-300;当 rep = 1 时,id 范围为 1-300; when rep = 2, id again ranges from 1-300 and so on.当 rep = 2 时,id 再次在 1-300 之间,依此类推。

I want to get the following: when rep = 1: id 1-300;我想得到以下信息:当 rep = 1: id 1-300; when rep = 2: id 301-600 and so on.当 rep = 2 时:id 301-600 等等。 This can be easily done using an if-else statement if number of replicates is relatively small - like the following code does the job for four replicates:如果复制的数量相对较少,这可以使用 if-else 语句轻松完成 - 就像下面的代码完成四次复制的工作:

d1 <- mutate(d1, ID = ifelse(rep==1, id,
ifelse(rep==2, id+300,
ifelse(rep==3, id+600, id+900))))

But how should I address this when I have 500 replicates?但是当我有 500 个重复时,我应该如何解决这个问题? So essentially my question is: how should I code - for every unit increase in replicate column, the id column will increase by 300?所以基本上我的问题是:我应该如何编码 - 对于复制列中的每个单位增加,id 列将增加 300? I have attached the result for 4 replicates (the result of the above code).我附上了 4 次重复的结果(上述代码的结果)。

Here is a snapshot of the data: replicate 1 replicate 4这是数据的快照:复制 1复制 4

I would use rep to identify the id , like:我会使用rep来识别id ,例如:

d1 <- mutate(d1, ID = id + 300*(rep-1))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 增加嵌套在R +的另一列中的一个变量,并将0设置为起始值 - increase in one variable nested within another column in R + setting 0 as starting value 基于条件 R 的列值顺序增加 - Sequential Increase in Column value based on a condition R Select 唯一条目显示来自另一列的至少一个值 - Select unique entries showing at least one value from another column R:将一列中的每个不同值合并到另一列中 - R: unite every distinct value in one column into another 根据条件将一列中的条目替换为另一列 - Replacing entries in one column by another based on condition 如何根据条件删除一列的值,增加其他列的数量 - How to delete the value of a column according to the conditions and increase the number of other columns 超过指定值时如何对列执行百分比增加 - How to perform a percentage increase on a column when surpassing a specified value 时间序列,找出一列中的值每天在另一列中的值之后减少多少 - Time series, find how much value in one column decreases after the value in another column every day 根据另一列(主题)的值,将一列中的每个值除以最大值 - Divide every value in one column by the largest value based on the values of another column (subject) Overwrite a column in one dataframe with one in another dataframe if the value for the entries in the second dataframe is among the top 50% in R - Overwrite a column in one dataframe with one in another dataframe if the value for the entries in the second dataframe is among the top 50% in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM