-1

Please does anyone know why the script below does not maintain the original order of the 'father' column? It rearranges the column and the trio would not match anymore. The data test has 3 columns (Child, father, mother). It is related to a previous post: Substitute the specific proportions of each group with another value in R Thank you.

test$father <- unlist(
tapply(test$father,test$father,
    function(x) {
            x[1:floor(length(x)*0.25)] <- 0
            x
            }
    )
)
Community
  • 1
  • 1
nolyugo
  • 1,451
  • 3
  • 12
  • 12
  • 2
    Please make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Also, include your output and your desired output. When I run this, it seems to work correctly. Try assigning to `test$father_new` instead so you can compare. – Justin Sep 11 '12 at 19:57
  • You are correct, it works well with a small data set but rearranges with a large data set, e.g. 6000 rows – nolyugo Sep 11 '12 at 20:08

1 Answers1

2

Your actual input data is probably not sorted by father while the test data in the linked question was.

You will find the new test$father is based on the sorted list.

Henry
  • 6,704
  • 2
  • 23
  • 39
  • It works well with a small data set but rearranges as the data gets large. Is there a way for me to prevent it from sorting? I will like it to maintain its original order. – nolyugo Sep 11 '12 at 20:06
  • If your original data set is sorted by `child` then one way to do it would be to precede the quoted code by `test <- test[order(test$father,test$child),]` and follow it by `test <- test[order(test$child),]` – Henry Sep 11 '12 at 22:46