[R] Issue replacing dataset values from read data
Chang, Emily
Emily.Chang2 at ucsf.edu
Sat May 7 00:19:13 CEST 2016
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]]
More information about the R-help
mailing list