Dear all,

I am reading a modest dataset (2297 x 644) with specific values I want to 
change. The code is inelegant but looks like this:

df <- read.csv("mydata.csv", header = TRUE, stringsAsFactors = FALSE)

# yrsquit, packyrs missing for following IDs. Manually change.
for(myid in c(2165, 2534, 2553, 2611, 2983, 3233)){
     temp <- subset(df, id == myid)
     df[df$id == myid , "yrsquit"] <- 0
     temp.yrssmoke <- temp$age-(temp$agesmoke+temp$yrsquit)
     df[df$id == myid , "yrssmoke"]  <- temp.yrssmoke
     df[df$id == myid , "packyrs"] <- (temp$cigsdaytotal/20)*(temp.yrssmoke)
}

If I run just the first line and then the for loop, it works.
If I run the first line and for loop together, yrsquit is properly replaced to 
== 0, but packyrs is NA still.

Obviously there's many ways around this specific problem, but I was wondering 
what the issue is here, so as to look out for and avoid it in the future.

Apologies for the lack of reproducible code; I haven't yet reproduced the 
problem with generated data.

Much thanks in advance.

Best regards,
Emily

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to