Message-ID: <CAA99HCwQFdaeE+gQiBMjiCAcA__a0NNJWSg+rk9CCc-2=i6h3g@mail.gmail.com>
Date: 2016-05-07T11:13:25Z
From: William Michels
Subject: Issue replacing dataset values from read data
In-Reply-To: <E8628E91E7FC33438D6681B3341266E2016A170E@ex09.net.ucsf.edu>
1. It's not immediately clear why you need the line "temp <- subset(df, id
== myid)"
2. The objects described by "temp$age", temp$agesmoke, and temp$yrsquit are
all vectors. So temp.yrssmoke is also a vector. This means that when you
replace, it should be with "<- temp.yrssmoke[i]", where "i" is the (row)
number you're looping over (note "temp" re-numbers rows to 1 through 6,
another reason to remove the "temp" line).
3. Ditto for " <- (temp$cigsdaytotal[i]/20)*(temp.yrssmoke[i]) "
Hope this helps!
Bill
W. Michels, Ph.D.
On Fri, May 6, 2016 at 3:19 PM, Chang, Emily <Emily.Chang2 at ucsf.edu> wrote:
> 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 at 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.
>
[[alternative HTML version deleted]]