[R] Assign value to a row in dataframe

Jim Lemon drjimlemon at gmail.com
Wed Jan 21 00:20:05 CET 2015


Hi Zuzana,
>From your description it seems that you want to repeat the last
"state_new" value when there are repeated zeros in both "state" and
"state_bef". If that is the case, you may need to step through the
data frame like this:

for(drow in 1:(dim(test)[1])) {
 if(test$state[drow] != 0) test$state_new[drow]<-test$state[drow]
 else {
  if(test$state_bef[drow] != 0) test$state_new[drow]<-test$state_bef[drow]
  else test$state_new[drow]<-test$state_new[drow-1]
 }
}

This assumes that "state" and "state_bef" will never both be zero in
the first row. If this might happen, you will have to add a "memory"
variable (e.g. "state_last"), initialize it to whatever value you want
and then set it equal to "state_new" at the end of each step.

It may be possible to do this as you were attempting above using
something like "rle", but I think it will be difficult.

Jim


On Wed, Jan 21, 2015 at 9:14 AM, zuzana zajkova <zuzulaz at gmail.com> wrote:
> Hi,
>
> I have probably basic question, but I would be happy if somebody could help
> me with this:
>
> I have a dataframe where I want to assign a value to other column copying
> the value in previous row under some condition.
>
> Here is an example (dput on the end of message):
>
>> test[278:290,]
>                datetime state state_bef state_new
> 279 2011-11-12 08:24:57     a         0         a
> 280 2011-11-12 08:31:42     b         a         b
> 281 2011-11-13 00:00:00     0         b         b
> 282 2011-11-14 00:00:00     0         0         b
> 283 2011-11-15 00:00:00     0         0         0
> 284 2011-11-16 00:00:00     0         0         0
> 285 2011-11-17 00:00:00     0         0         0
> 286 2011-11-18 00:00:00     0         0         0
> 287 2011-11-19 00:00:00     0         0         0
> 288 2011-11-19 06:13:18     a         0         a
> 289 2011-11-19 06:40:06     b         a         b
> 290 2011-11-19 08:56:54     a         b         a
> 291 2011-11-19 09:19:39     b         a         b
>
>
> What I need is:
> if state != "0", the state_new==state
> if state == "0" & state_bef!="0", the state_new==state_bef
> if state == "0" & state_bef=="0", the state_new==state_bef of the previous
> row.
> The idea is when there is "00:00:00" in datetime, I need to repeat the last
> "state" value in the "state_new"
>
> I have tried various ifelse conditions and loops, the last is this, which
> doesn't work for all rows:
>
> test$state_new[test$state!="0"] <- test$state[test$state!="0"]
> test$state_new[test$state=="0" & test$state_bef!="0"] <-
> test$state_bef[test$state=="0" & test$state_bef!="0"]
> test$state_new[test$state=="0" & test$state_bef=="0"] <-
> test$state_new[test$state=="0" & test$state_bef=="0"]  # doesn't work for
> all rows...
>
> This should be the result
>
>  datetime state state_bef state_new
> 279 2011-11-12 08:24:57     a         0         a
> 280 2011-11-12 08:31:42     b         a         b
> 281 2011-11-13 00:00:00     0         b         b
> 282 2011-11-14 00:00:00     0         0         b
> 283 2011-11-15 00:00:00     0         0         b
> 284 2011-11-16 00:00:00     0         0         b
> 285 2011-11-17 00:00:00     0         0         b
> 286 2011-11-18 00:00:00     0         0         b
> 287 2011-11-19 00:00:00     0         0         b
> 288 2011-11-19 06:13:18     a         0         a
> 289 2011-11-19 06:40:06     b         a         b
> 290 2011-11-19 08:56:54     a         b         a
> 291 2011-11-19 09:19:39     b         a         b
>
>
> In the dataframe I send as an example there is just one block of this
> repeated "0" values, but within real dataframe there are more blocks like
> that.
>
> Thanks for any ideas!
>
> Best regards,
>
> Zuzana
>
>
>
>
>
>
>> dput(test)
> structure(list(datetime = structure(c(1320264600, 1320265473,
> 1320266409, 1320266418, 1320266460, 1320267591, 1320268350, 1320268371,
> 1320276948, 1320277332, 1320278400, 1320280380, 1320283950, 1320283977,
> 1320286293, 1320287010, 1320287022, 1320287055, 1320287604, 1320288678,
> 1320295476, 1320296490, 1320296766, 1320300015, 1320319140, 1320320367,
> 1320331629, 1320333789, 1320354315, 1320355689, 1320360000, 1320364800,
> 1320366489, 1320378507, 1320378702, 1320393177, 1320394809, 1320412825,
> 1320413140, 1320417361, 1320418849, 1320418855, 1320419854, 1320419860,
> 1320420244, 1320420268, 1320420277, 1320451200, 1320452443, 1320455566,
> 1320455716, 1320455839, 1320455845, 1320455851, 1320455896, 1320456094,
> 1320456109, 1320457492, 1320458053, 1320460879, 1320465739, 1320465748,
> 1320466018, 1320469426, 1320469468, 1320475630, 1320476410, 1320482851,
> 1320489187, 1320490969, 1320495052, 1320495061, 1320519571, 1320519580,
> 1320519610, 1320520594, 1320520600, 1320520618, 1320520624, 1320520636,
> 1320526588, 1320527017, 1320537600, 1320554581, 1320554911, 1320554926,
> 1320554941, 1320571210, 1320573523, 1320608509, 1320609532, 1320611587,
> 1320616504, 1320616513, 1320616768, 1320618364, 1320620602, 1320620833,
> 1320622147, 1320623194, 1320624000, 1320632149, 1320632164, 1320632446,
> 1320646327, 1320646810, 1320663212, 1320663716, 1320665483, 1320665870,
> 1320666332, 1320666665, 1320666680, 1320666689, 1320666899, 1320666968,
> 1320668141, 1320668147, 1320669017, 1320669545, 1320670070, 1320670076,
> 1320670118, 1320670130, 1320670154, 1320670166, 1320670175, 1320670181,
> 1320670232, 1320670244, 1320670262, 1320670700, 1320671432, 1320671891,
> 1320672518, 1320673208, 1320673478, 1320673484, 1320673556, 1320673628,
> 1320673706, 1320673739, 1320673751, 1320673769, 1320673775, 1320673799,
> 1320673817, 1320673835, 1320673853, 1320674531, 1320675434, 1320675968,
> 1320676139, 1320676154, 1320676592, 1320677327, 1320677822, 1320678038,
> 1320678047, 1320678179, 1320678185, 1320678533, 1320679199, 1320679535,
> 1320679718, 1320682901, 1320683333, 1320684239, 1320684248, 1320684362,
> 1320685019, 1320685346, 1320685718, 1320686645, 1320710400, 1320711704,
> 1320712352, 1320721790, 1320722021, 1320722066, 1320722237, 1320730745,
> 1320730766, 1320730784, 1320730886, 1320730892, 1320730904, 1320730955,
> 1320731075, 1320731120, 1320731126, 1320731132, 1320731165, 1320731192,
> 1320731237, 1320731243, 1320731285, 1320731297, 1320731426, 1320731456,
> 1320731828, 1320731843, 1320731855, 1320731894, 1320731909, 1320731936,
> 1320731942, 1320731948, 1320732959, 1320739526, 1320739553, 1320747848,
> 1320747962, 1320752759, 1320757286, 1320774035, 1320774077, 1320780272,
> 1320781529, 1320796800, 1320809246, 1320809390, 1320809468, 1320809621,
> 1320809663, 1320811136, 1320811145, 1320812081, 1320812087, 1320812102,
> 1320812108, 1320812159, 1320812165, 1320812207, 1320812225, 1320812243,
> 1320812273, 1320812294, 1320812312, 1320812327, 1320812387, 1320812396,
> 1320812432, 1320812438, 1320873179, 1320879218, 1320881798, 1320883200,
> 1320883433, 1320886658, 1320889946, 1320893282, 1320893447, 1320904325,
> 1320905744, 1320943689, 1320944409, 1320948294, 1320948366, 1320949248,
> 1320949866, 1320950628, 1320952290, 1320953097, 1320954528, 1320954543,
> 1320954549, 1320958215, 1320961533, 1320969600, 1320972552, 1320973902,
> 1320974547, 1320974931, 1320985044, 1320985257, 1321056000, 1321086297,
> 1321086702, 1321142400, 1321228800, 1321315200, 1321401600, 1321488000,
> 1321574400, 1321660800, 1321683198, 1321684806, 1321693014, 1321694379
> ), class = c("POSIXct", "POSIXt"), tzone = "GMT"), state = c("b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "0", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "0", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "0", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "0", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "0", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "0", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "0", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "0",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "0", "a", "b", "a", "b",
> "a", "b", "0", "a", "b", "0", "0", "0", "0", "0", "0", "0", "a",
> "b", "a", "b"), state_bef = c("0", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "0", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "0",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "0", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "0", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "0", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "0",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "0", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "0", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "0", "a", "b", "a", "b", "a", "b", "0", "a", "b",
> "0", "0", "0", "0", "0", "0", "0", "a", "b", "a"), state_new = c("b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "a",
> "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b",
> "a", "b", "a", "b", "a", "b", "a", "b", "b", "a", "b", "a", "b",
> "a", "b", "b", "a", "b", "b", "b", "0", "0", "0", "0", "0", "a",
> "b", "a", "b")), .Names = c("datetime", "state", "state_bef",
> "state_new"), row.names = 2:291, class = "data.frame")
>
> <zuzanazajkova at ub.edu>
>
>         [[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.



More information about the R-help mailing list