R中的随机列循环 [英] Randomised column loop in R

查看:58
本文介绍了R中的随机列循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个数据帧,其中有一系列列,其中包含另一列的随机分配.数据具有一些需要维护的结构.也就是说,我想多次随机分配 L 的分配,同时将结构保持为 V .我想要一个看起来像这样的数据框;

I am trying to build a data frame where I have a series of columns which contain a random assignment of another column. The data has some structure which needs to be maintained. Namely I want to randomise the assignment of L many time over, while maintaining the structure in V. I want to take a dataframe that looks like this;

  L B V    A
1 1 1 2 10.9
2 1 1 2  6.5
3 1 1 2  8.6
4 1 1 3 11.1
5 1 1 4 13.1
6 1 1 6 11.5

并创建它;

     ID L B V    A R1 R2 R3 R4 R5
1 1_1_2 1 1 2 10.9 27 20 19  6 26
2 1_1_2 1 1 2  6.5 27 20 19  6 26
3 1_1_2 1 1 2  8.6 27 20 19  6 26
4 1_1_3 1 1 3 11.1  6 28  4 26 26
5 1_1_4 1 1 4 13.1 16  2  6 14 32
6 1_1_6 1 1 6 11.5 17 21  3 11 25

我可以使用下面的脚本手动进行此操作,但是我想知道是否有一种使之自动化的平滑方法,因为我想对数百个随机化进行处理,以使列R1,R2,R3 .. R n (因此,执行此循环比手动重复执行代码更可取.)

I can do this manually using the below script, but I wonder if there is a smooth way to make this automated, because I want to do it for hundreds of randomisations to make columns R1, R2, R3.. Rn (so a loop to do this would be preferred to manual repetition of the code).

# Example Data Frame #
df = data.frame(sample(1:33, 1000, replace = T), sample(1:3, 1000, replace = T), sample(1:9, 1000, replace = T), round(rnorm(1000, 10, 2),1))
colnames(df) = c("L", "B", "V", "A")
df = transform(df,id=as.numeric(factor(df$V)))
df = data.frame(as.factor(df[,1]),as.factor(as.numeric(df[,2])),as.factor(df[,5]),as.numeric(df[,4]))
colnames(df) = c("L","B","V","A")
df = df[order(df$L, df$B, df$V),]
rownames(df) = NULL
head(df)

# ID #
df$ID = paste(df[,1], df[,2], df[,3], sep = "_")
ID = unique(as.vector(df$ID))

# R1 #
ID2 = data.frame(ID, sample(ID)); colnames(ID2) = c("ID","R1")
df = merge(df, ID2)
df$R1 = as.factor(do.call(rbind, strsplit(as.vector(df$R1), split="_"))[,1])

# R2 #
ID2 = data.frame(ID, sample(ID)); colnames(ID2) = c("ID","R2")
df = merge(df, ID2)
df$R2 = as.factor(do.call(rbind, strsplit(as.vector(df$R2), split="_"))[,1])

# R3 #
ID2 = data.frame(ID, sample(ID)); colnames(ID2) = c("ID","R3")
df = merge(df, ID2)
df$R3 = as.factor(do.call(rbind, strsplit(as.vector(df$R3), split="_"))[,1])

# R4 #
ID2 = data.frame(ID, sample(ID)); colnames(ID2) = c("ID","R4")
df = merge(df, ID2)
df$R4 = as.factor(do.call(rbind, strsplit(as.vector(df$R4), split="_"))[,1])

# R5 #
ID2 = data.frame(ID, sample(ID)); colnames(ID2) = c("ID","R5")
df = merge(df, ID2)
df$R5 = as.factor(do.call(rbind, strsplit(as.vector(df$R5), split="_"))[,1])

如何创建一个循环,使之在 n 列数内发生?

How can I create a loop which will make this happen in n number of columns?

推荐答案

在上面的代码之后,我终于得到了答案:

Following from the above code I finally got to an answer:

# ID #
df$ID = paste(df[,1], df[,2], df[,3], sep = "_")
ID = unique(as.vector(df$ID))

n = 5
Rs     = as.vector(rep(NA,n))

for(i in 1:n){
 Rs[i] =     paste("R",i, sep = "")
 }
Rs

for(i in 1:n){
df[,5+i] = NA
colnames(df)[5+i] = paste(Rs[i])
ID = unique(as.vector(df$ID))
ID2 = data.frame(ID, sample(ID))
ID2 = merge(df, ID2)
df[5+i] = as.factor(do.call(rbind, strsplit(as.vector(ID2[,6+i]), split="_"))[,1])
}
head(df)

给出结果:

  L B V    A    ID R1 R2 R3 R4 R5
1 1 1 2 10.1 1_1_2 21 12 27  4 26
2 1 1 4  7.7 1_1_4  7 29  2  9 10
3 1 1 5  9.7 1_1_5 27 27  3  1 22
4 1 1 5  8.3 1_1_5 27 27  3  1 22
5 1 1 7  9.5 1_1_7 13 15 32 19 11
6 1 1 7 12.4 1_1_7 13 15 32 19 11

这篇关于R中的随机列循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆