Hi Rui, Data as follows. These data are arranged in choice sets of 6.
head(dat,50) dn obs choice br mth 1 4 1 0 1 NA 2 4 1 0 2 NA 3 4 1 0 3 NA 4 4 1 0 4 NA 5 4 1 0 5 NA 6 4 1 1 6 487 7 4 2 0 1 NA 8 4 2 0 2 NA 9 4 2 1 3 488 10 4 2 0 4 NA 11 4 2 0 5 NA 12 4 2 0 6 NA 13 4 3 0 1 NA 14 4 3 0 2 NA 15 4 3 0 3 NA 16 4 3 0 4 NA 17 4 3 0 5 NA 18 4 3 1 6 488 19 4 4 0 1 NA 20 4 4 0 2 NA 21 4 4 1 3 489 22 4 4 0 4 NA 23 4 4 0 5 NA 24 4 4 0 6 NA 25 4 5 0 1 NA 26 4 5 0 2 NA 27 4 5 0 3 NA 28 4 5 0 4 NA 29 4 5 0 5 NA 30 4 5 1 6 489 31 4 6 0 1 NA 32 4 6 0 2 NA 33 4 6 0 3 NA 34 4 6 0 4 NA 35 4 6 0 5 NA 36 4 6 1 6 489 37 4 7 0 1 NA 38 4 7 0 2 NA 39 4 7 0 3 NA 40 4 7 0 4 NA 41 4 7 0 5 NA 42 4 7 1 6 490 43 4 8 0 1 NA 44 4 8 0 2 NA 45 4 8 0 3 NA 46 4 8 0 4 NA 47 4 8 0 5 NA 48 4 8 1 6 491 49 4 9 0 1 NA 50 4 9 0 2 NA Best wishes Graham -----Original Message----- From: Rui Barradas [mailto:ruipbarra...@sapo.pt] Sent: 06 April 2013 16:32 To: Leask, Graham Cc: r-help@r-project.org Subject: Re: [R] Replace missing value within group with non-missing value Hello, Can't you post a data example? If your dataset is named 'dat' use dput(head(dat, 50)) # paste the output of this in a post Rui Barradas Em 06-04-2013 15:34, Leask, Graham escreveu: > Hi Rui, > > Thank you for your suggestion which is very much appreciated. Unfortunately > running this code produces the following error. > > error in '$<-.data.frame' ('*tmp*', "mth", value = NA_real_) : > replacement has 1 rows, data has 0 > > I'm sure there must be an elegant solution to this problem? > > Best wishes > > > > Graham > > On 6 Apr 2013, at 12:15, "Rui Barradas" <ruipbarra...@sapo.pt> wrote: > >> Hello, >> >> That's not a very good way of posting your data, preferably paste the output >> of ?dput in a post. >> Some thing along the lines of the following might do what you want. >> It seems that the groups are established by 'dn' and 'obs' numbers. >> If so, try >> >> >> # Make up some data >> dat <- data.frame(dn = 4, obs = rep(1:5, each = 6), mth = NA) >> dat$mth[6] <- 487 dat$mth[9] <- 488 dat$mth[18] <- 488 dat$mth[21] <- >> 489 dat$mth[30] <- 489 >> >> >> sp <- split(dat, list(dat$dn, dat$obs)) >> names(sp) <- NULL >> tmp <- lapply(sp, function(x){ >> idx <- which(!is.na(x$mth))[1] >> x$mth <- x$mth[idx] >> x >> }) >> do.call(rbind, tmp) >> >> >> Hope this helps, >> >> Rui Barradas >> >> >> Em 06-04-2013 11:33, Leask, Graham escreveu: >>> Dear List members >>> >>> I have a large dataset organised in choice groups see sample below >>> >>> >>> +-------------------------------------------------------------------------------------------------+ >>> | dn obs choice acid br date cdate >>> situat~n mth year set | >>> >>> |-------------------------------------------------------------------------------------------------| >>> 1. | 4 1 0 LOSEC 1 . . >>> . . 1 | >>> 2. | 4 1 0 NEXIUM 2 . . >>> . . 1 | >>> 3. | 4 1 0 PARIET 3 . . >>> . . 1 | >>> 4. | 4 1 0 PROTIUM 4 . . >>> . . 1 | >>> 5. | 4 1 0 ZANTAC 5 . . >>> . . 1 | >>> >>> |-------------------------------------------------------------------------------------------------| >>> 6. | 4 1 1 ZOTON 6 23aug2000 01:00:00 23aug2000 >>> NS 487 2000 1 | >>> 7. | 4 2 0 LOSEC 1 . . >>> . . 2 | >>> 8. | 4 2 0 NEXIUM 2 . . >>> . . 2 | >>> 9. | 4 2 1 PARIET 3 25sep2000 01:00:00 25sep2000 >>> L 488 2000 2 | >>> 10. | 4 2 0 PROTIUM 4 . . >>> . . 2 | >>> >>> |-------------------------------------------------------------------------------------------------| >>> 11. | 4 2 0 ZANTAC 5 . . >>> . . 2 | >>> 12. | 4 2 0 ZOTON 6 . . >>> . . 2 | >>> 13. | 4 3 0 LOSEC 1 . . >>> . . 3 | >>> 14. | 4 3 0 NEXIUM 2 . . >>> . . 3 | >>> 15. | 4 3 0 PARIET 3 . . >>> . . 3 | >>> >>> |-------------------------------------------------------------------------------------------------| >>> 16. | 4 3 0 PROTIUM 4 . . >>> . . 3 | >>> 17. | 4 3 0 ZANTAC 5 . . >>> . . 3 | >>> 18. | 4 3 1 ZOTON 6 20sep2000 00:00:00 20sep2000 >>> R 488 2000 3 | >>> 19. | 4 4 0 LOSEC 1 . . >>> . . 4 | >>> 20. | 4 4 0 NEXIUM 2 . . >>> . . 4 | >>> >>> |-------------------------------------------------------------------------------------------------| >>> 21. | 4 4 1 PARIET 3 27oct2000 00:00:00 27oct2000 >>> NL 489 2000 4 | >>> 22. | 4 4 0 PROTIUM 4 . . >>> . . 4 | >>> 23. | 4 4 0 ZANTAC 5 . . >>> . . 4 | >>> 24. | 4 4 0 ZOTON 6 . . >>> . . 4 | >>> 25. | 4 5 0 LOSEC 1 . . >>> . . 5 | >>> >>> |-------------------------------------------------------------------------------------------------| >>> 26. | 4 5 0 NEXIUM 2 . . >>> . . 5 | >>> 27. | 4 5 0 PARIET 3 . . >>> . . 5 | >>> 28. | 4 5 0 PROTIUM 4 . . >>> . . 5 | >>> 29. | 4 5 0 ZANTAC 5 . . >>> . . 5 | >>> 30. | 4 5 1 ZOTON 6 23oct2000 03:00:00 23oct2000 >>> NS 489 2000 5 | >>> >>> I wish to fill in the missing values in each choice set - delineated by dn >>> (Doctor) obs (Observation number) and choices (1 to 6). >>> For each choice set one choice is chosen which contains full time >>> information for that choice set ie in set 1 choice 6 was chosen and shows >>> the month 487. The other 5 choices show mth as missing. I want to fill >>> these with the correct mth. >>> >>> I am sure there must be an elegant way to do this in R? >>> >>> >>> Best wishes >>> >>> >>> >>> Graham >>> >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> R-help@r-project.org mailing list >>> 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. >> > ______________________________________________ R-help@r-project.org mailing list 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.