[R] Assign value to a row in dataframe

zuzana zajkova zuzulaz at gmail.com
Wed Jan 21 12:50:52 CET 2015


Hi Petr,

your solution works also perfectly, thanks a lot! Again, I have learned new
things :)

(Sorry for problems with the dput...)

Best wishes,

Zuzana


<zuzanazajkova at ub.edu>

On 21 January 2015 at 11:57, PIKAL Petr <petr.pikal at precheza.cz> wrote:

> Hi
>
> I am not sure if I catch it correctly but from your example state has
> preference over state new except in the case where state is 0.
>
> I would change state_bef 0 to NA and use na.locf function from zoo package.
>
> test$state_bef[test$state_bef=="0"] <- NA
> test$state_bef <- na.locf(test$state_bef, na.rm=FALSE)
> test$state_new <- ifelse(test$state == "0", as.character(test$state_bef),
> as.character(test$state))
>
> library(zoo)
>
> # as you failed to use dput for data I need to do some transformation
> before I got appropriate object.
>
> temp<-read.delim("clipboard", header=F, sep=" ")
> temp<-t(temp)
> temp<-t(temp[complete.cases(temp),])
> temp<-data.frame(temp)
> names(temp)[4:6]<-c("state", "state_bef", "state_new")
>
>
> #  here is the code
> temp$state_bef[temp$state_bef=="0"] <- NA
> temp$state_bef <- na.locf(temp$state_bef, na.rm=FALSE)
> temp$state_new <-  ifelse(temp$state == "0", as.character(temp$state_bef),
> as.character(temp$state))
> temp
>     V1         V2       V3 state state_bef state_new
> 1  279 2011-11-12 08:24:57     a      <NA>         a
> 2  280 2011-11-12 08:31:42     b         a         b
> 3  281 2011-11-13 00:00:00     0         b         b
> 4  282 2011-11-14 00:00:00     0         b         b
> 5  283 2011-11-15 00:00:00     0         b         b
> 6  284 2011-11-16 00:00:00     0         b         b
> 7  285 2011-11-17 00:00:00     0         b         b
> 8  286 2011-11-18 00:00:00     0         b         b
> 9  287 2011-11-19 00:00:00     0         b         b
> 10 288 2011-11-19 06:13:18     a         b         a
> 11 289 2011-11-19 06:40:06     b         a         b
> 12 290 2011-11-19 08:56:54     a         b         a
> 13 291 2011-11-19 09:19:39     b         a         b
> >
>
> which results in desired solution.
>
> Cheers
> Petr
>
>
> > -----Original Message-----
> > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of zuzana
> > zajkova
> > Sent: Tuesday, January 20, 2015 11:15 PM
> > To: r-help at r-project.org
> > Subject: [R] Assign value to a row in dataframe
> >
> > 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.
>
> ________________________________
> Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou
> určeny pouze jeho adresátům.
> Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě
> neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie
> vymažte ze svého systému.
> Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email
> jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
> Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi
> či zpožděním přenosu e-mailu.
>
> V případě, že je tento e-mail součástí obchodního jednání:
> - vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření
> smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
> - a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout;
> Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany
> příjemce s dodatkem či odchylkou.
> - trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve
> výslovným dosažením shody na všech jejích náležitostech.
> - odesílatel tohoto emailu informuje, že není oprávněn uzavírat za
> společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn
> nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto
> emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich
> existence je adresátovi či osobě jím zastoupené známá.
>
> This e-mail and any documents attached to it may be confidential and are
> intended only for its intended recipients.
> If you received this e-mail by mistake, please immediately inform its
> sender. Delete the contents of this e-mail with all attachments and its
> copies from your system.
> If you are not the intended recipient of this e-mail, you are not
> authorized to use, disseminate, copy or disclose this e-mail in any manner.
> The sender of this e-mail shall not be liable for any possible damage
> caused by modifications of the e-mail or by delay with transfer of the
> email.
>
> In case that this e-mail forms part of business dealings:
> - the sender reserves the right to end negotiations about entering into a
> contract in any time, for any reason, and without stating any reasoning.
> - if the e-mail contains an offer, the recipient is entitled to
> immediately accept such offer; The sender of this e-mail (offer) excludes
> any acceptance of the offer on the part of the recipient containing any
> amendment or variation.
> - the sender insists on that the respective contract is concluded only
> upon an express mutual agreement on all its aspects.
> - the sender of this e-mail informs that he/she is not authorized to enter
> into any contracts on behalf of the company except for cases in which
> he/she is expressly authorized to do so in writing, and such authorization
> or power of attorney is submitted to the recipient or the person
> represented by the recipient, or the existence of such authorization is
> known to the recipient of the person represented by the recipient.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list