gpt4 book ai didi

R 循环 :conditioning a loop in R

转载 作者:行者123 更新时间:2023-12-04 09:18:12 27 4
gpt4 key购买 nike

感谢您的反馈,下面是我想要的输出的可重现示例:

# Example Data where I would like my output
N=24
school.assignment = matrix(NA, ncol = 3, nrow = N)
school.assignment = as.data.frame(school.assignment)
colnames(school.assignment) <- c("ID","Group","Assignment")

# Number of groups and assigments per group
groups = 6
Assignment = 4
school.assignment$Group<-rep(1:groups,Assignment)
school.assignment$Group<- sort(school.assignment$Group)
school.assignment$Assignment<-rep(1:Assignment)


# IDs with number of repeats (i.e repeated students)
Data = matrix(0, ncol = 2, nrow = N/2) # ~half with repeated samples
Data = as.data.frame(Data)
colnames(Data) <- c("ID","Repeats")
Data$ID <-1:(N/2)
length(unique(Data$ID)) # unique IDS
ID=rep(seq(1:8),3)

# Genearte random repeats for each ID
Data$Repeats<-{set.seed(55)
sapply(1:(N/2),
function(x) sample(1:5,1))
}
Data=Data[-1,] #take out first row to match N=24
sum(Data$Repeats) #24 total IDs for all assigments


# List of IDs at random to use
IDs <- vector("list",dim(Data)[1]) #
for (i in 1:dim(Data)[1])
{
IDs[[i]]<-rep(Data$ID[i], times=Data$Repeats[i])
}
head(IDs)


# Object with number of repeated IDs
sample.per.ID <- vector("list",length(IDs)[1])
for (i in 1:length(IDs))
{
sample.per.ID[[i]]<-sum(length((IDs)[[i]]))
}
sum=sum(as.data.frame(sample.per.ID)); sum # 24 total IDs (including repeats)

## Unlist Vector with ransom sequence of samples
SRS.ID.order = unlist(IDs) #order of IDs with repeats
for (i in 1:length(SRS.ID.order ))
{
school.assignment$ID[i]<-SRS.ID.order [i]
}

我的最后一个循环是尝试将 ID 分配给我的 school.assignment$ID 矩阵。但是,如您所见,一些 ID 跨越不同的组,我想从 SRS.ID.order 条件分配 ID 以保持在同一组内(即常量 school.assignment$Group,在下面你可以看到情况并非如此,例如 ID 4 在组 1 和组 2 中)

> head(school.assignment)
ID Group Assignment
1 2 1 1
2 2 1 2
3 3 1 3
4 4 1 4
5 4 2 1
6 4 2 2

如果下一个 school.assignment$ID 长度比该组中的可用空间长,我希望循环的输出不向该组分配任何 ID(即 NA)。

  ID Group Assignment
1 2 1 1
2 2 1 2
3 3 1 3
4 NA 1 4
5 4 2 1
6 4 2 2

我在想我需要某种类型的 J 组指标,如下面的代码:

########################################
for (i in 1:length(school.assignment$ID))
{
for (j in 1:length(unique(school.assignment$Group)))
{
school.assignment$ID[i]<-ifelse(sum(is.na(school.assignment$ID[i,j]))>=sample.per.ID[i],SRS.ID.order[i],NA)
}
}
Error in school.assignment$ID[i, j] : incorrect number of dimensions

非常感谢任何帮助!

谢谢


旧帖

我目前正尝试在 R 中执行一个带有条件的循环。我的数据结构如下:

> head(school.assignment)
ID Group Assignment
1 NA 1 1
2 NA 1 2
3 NA 1 3
4 NA 1 4
5 NA 2 1
6 NA 2 2

我想将与 school.assignment 相同长度的 ID 分配给如下所示的 ID 变量:

head(IDs)
[1] 519 519 519 343 251 251...

并非所有 ID 都重复相同的次数,大约 1,2 次甚至 3 次,如上所示。我有一个包含每个 ID 重复次数的对象,例如:

> head(repeats)
[1] 3 1 2...

表示ID=519重复3次,ID=343只重复一次广告ID=251重复2次等...

我想满足一个条件:

1) 我希望每个 ID 都尽可能在同一组中(即,如果矩阵对象“school.assignment”中的 ID 只剩下一个点 (NA)第 1 组然后将 ID 分配给下一组,在那里它们将有足够的空间(即 NA for school.assignment$ID is >= to repeats for that ID)

我的想法是做一个循环,但下面的代码不起作用:

########################################
for (i in 1:length(school.assignment$ID))
{
for (j in 1:length(unique(school.assignment$Group)))
{
school.assignment$ID[i]<-ifelse(sum(is.na(school.assignment$ID[i,j]))>=repeats[i],ID[i],NA)
}
}

有没有一种方法可以执行此循环,同时遵守我将 ID 仅分配给一个组的条件?

谢谢!

最佳答案

考虑使用 merge()将随机组 ID 分配给数据帧。不需要嵌套的 for 循环。下面创建一个唯一的组数据框,在那里分配随机数,然后与 school.assignment 合并:

# CREATE UNIQUE GROUP DATA FRAME 
Group <- unique(school.assignment$Group)
grp.ids <- as.data.frame(Group)

# CREATE RANDOM ID FIELD (THREE DIGITS BETWEEN 100 AND 999)
grp.ids$RandomID <- sample(100:999, size = nrow(grp.ids), replace = TRUE)

# MERGE DATA FRAMES
school.assignment <- merge(school.assignment, grp.ids, by="Group", all=TRUE)
# ASSIGN ID COLUMN
school.assignment$ID <- school.assignment$RandomID
# RESTRUCTURE FINAL DATA FRAME
school.assignment <- school.assignment[c("ID", "Group", "Assignment")]

输出

 ID     Group   Assignment
977 1 1
977 1 2
977 1 3
977 1 4
368 2 1
368 2 2

关于R 循环 :conditioning a loop in R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34242283/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com