Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环仅在R中的最后一次迭代中运行-在参与者上循环_R - Fatal编程技术网

循环仅在R中的最后一次迭代中运行-在参与者上循环

循环仅在R中的最后一次迭代中运行-在参与者上循环,r,R,我对R非常陌生,我正在尝试运行一个循环,所以非常感谢您的帮助 我有每个参与者的多个时间点的纵向数据,看起来像附件中的图片 我需要将NA值替换为Years变量等于0时的值,我想为每个参与者编写一个循环。我已经编写了一些代码,似乎可以工作,但是它只为循环的最后一次迭代(最后一个参与者)提供输出。这是我正在使用的代码: x <- c(1:4) n = length(x) for(i in 1:n) { data <- subset(df, ID %in% c(x[i]))

我对R非常陌生,我正在尝试运行一个循环,所以非常感谢您的帮助

我有每个参与者的多个时间点的纵向数据,看起来像附件中的图片

我需要将
NA
值替换为
Years
变量等于0时的值,我想为每个参与者编写一个循环。我已经编写了一些代码,似乎可以工作,但是它只为循环的最后一次迭代(最后一个参与者)提供输出。这是我正在使用的代码:

x <- c(1:4) 

n = length(x)

for(i in 1:n)
{
  data <- subset(df, ID %in% c(x[i]))
  
  data$outcome <- ifelse(is.na(data$outcome), 
                         data[1,3], 
                   data$outcome)
}

x您的循环将替换每次迭代的
data$outcome
。这就是为什么你只能得到最后的结果

以下是我不雅观的解决方案:

使样本数据与您的匹配(不包括未使用的列)


my_dat更简单的tidyverse方法:

library(tidyverse)

df %>%
  filter(ID %in% x) %>%
  mutate(outcome = ifelse(is.na(Outcome), Years, Outcome))
  

你的问题需要一些澄清和一个值得谴责的例子。正如我所理解的:“我需要用年份变量等于0时的值替换NA值”。因此,如果
outcome
等于
NA
Years
等于
0
你想
outcom
等于
0

set.seed(1984) # ser the seed so that my_dat is the same each time
# using a modified df from markhogue answer...
my_dat <- data.frame(
  ID = 1:30,
  years = sample(c(0, 1.5, 3), 30, replace = T),
  outcome = as.numeric(sample(c("", 1, 2), 30, replace = T))
)
my_dat # have a look at rows 9 and 22
# ifelse given two conditions does year == 0 and is.na(outcome)
my_dat$outcome <- ifelse(my_dat$year == 0 & is.na(my_dat$outcome), my_dat$years, my_dat$outcome)
my_dat # have a look at rows 9 and 22
set.seed(1984)#设置种子,使我的数据每次都相同
#使用来自markhogue答案的修改df。。。

我的数据我不是100%清楚您的意图,但这将在一个ID内,用
年份==0的行中的(第一个)
结果
值填充所有
结果
缺失的值

library(dplyr)
df %>% 
  group_by(ID) %>%
  mutate(outcome = coalesce(outcome, first(outcomes[Years == 0])))

显然未经测试,但如果您提供一些示例数据,我很乐意帮助调试。

我相信
ifelse
已经进行了行计算。您可能不需要allAlway的循环,因为它是一个整洁的解决方案,并且在我测试它时可以工作。不过有点慢
microbenchmark(base=my_dat$outcome%group_by(ID)%%>%mutate(outcome=coalesce(outcome,first(outcome,first(outcome[years==0]))
@QAsena好吧,(a)我们对这个问题有不同的解释——只有当NA值恰好出现在
years==0的行上时,你才填写NA值。我对这个问题的理解是OP希望用
years==0的行中的值来填充ID中所有缺少的值。也许OP会澄清问题…(b)如果您没有对足够大的数据进行基准测试,以使差异至少达到0.01秒,那么这无关紧要(除非这是对时间敏感的生产代码…)。如果OP的目的确实是一个分组操作,我很有信心
dplyr
将比
base
更快地处理任何足够大的数据,使差异变得明显。当然,如果我真的关心速度,我会使用
data.table
)非常感谢大家的帮助!很抱歉,问题没有明确说明或澄清-但Gregor您的解释是正确的,您的解决方案工作得非常好!非常感谢你。@Gregor Thomas。我同意,关于速度的
data.table
(我已经看到
dplyr
在速度检查中被
base
击败,但在这一点上是逐案的!)。目前我对大数据集有很多速度和内存限制,所以我的想法是:D
library(tidyverse)

df %>%
  filter(ID %in% x) %>%
  mutate(outcome = ifelse(is.na(Outcome), Years, Outcome))
  
set.seed(1984) # ser the seed so that my_dat is the same each time
# using a modified df from markhogue answer...
my_dat <- data.frame(
  ID = 1:30,
  years = sample(c(0, 1.5, 3), 30, replace = T),
  outcome = as.numeric(sample(c("", 1, 2), 30, replace = T))
)
my_dat # have a look at rows 9 and 22
# ifelse given two conditions does year == 0 and is.na(outcome)
my_dat$outcome <- ifelse(my_dat$year == 0 & is.na(my_dat$outcome), my_dat$years, my_dat$outcome)
my_dat # have a look at rows 9 and 22
library(dplyr)
df %>% 
  group_by(ID) %>%
  mutate(outcome = coalesce(outcome, first(outcomes[Years == 0])))