Sunday 15 April 2012

r - randomize or permuting values in a data.frame -


I have a data Frame that looks like this: (My real dataframe is big):

  df < - data.fr (A = C ("A", "B", "C", "D", "E", "F", "G" "H", "I"), B = C (" 1 "," 1 "," 1 "," 2 "," 2 "," 2 "," 3 "," 3 "," 3 ") C = C (0.1,0.2,0.4,0.1,0.5,0.7 , 0.1.0.2,0.5))> & gt; DF ABC 1A1 0.1 2 B1 0.2 3 C 1 0.4 4 D2 0.1 5 E 2 0.5 6 F2 0.7 7 G3 0.1 8 H3 0.2 9 Ii 0.5   

I Want to add several n-columns (like permutations ), where the column D will be a random value from D df $ C but this value should be selected from those rows < With the best price of Code> df $ B , an example of the desired product would be:

  df < - data.frame (A = C ("A", "B", "B", "C", "D", "E", "F", "G", "H", "I") B = C ("1", "1", "1", "2", "2", "2", "3", "3", "3"), c = c (0.1.0.2.0.4, 0.1,0.5,0.7,0.1,0.2,0.5), D = C (0.20.2,0.1,0.5,0.7,0.1,0.5,0.5,0.2))> DF ABCD 1A1 0.1 0.2 2 B1 0.2 0.2 3 C1 0.4 0.1 4 D2 0.1 0.5 5 E2 0.5 0.7 6 F2 0.7 0.1 7 G3 0.1 0.5 8 H3 0.2 0.5 9 I5 0.5 0.2   

I have tried the package with plyr but my approach does not work properly:

  ddply (df, level (. (B)), conversion, D = sample (C))   

I also have thought of dividing dataframe based on df $ B and its Each data frame using lapply to add a later function But I have no indication that how to choose the df $ B level,

many thanks

plyr , ave

  transform (df, d = ave (c, b, fun = function (b) sample (b, replace = TRUE))    

No comments:

Post a Comment