I have a data Frame that looks like this: (My real dataframe is big): I Want to add several n-columns (like permutations ), where the column D will be a random value from D I have tried the package with I also have thought of dividing dataframe based on many thanks plyr ,
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
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
plyr but my approach does not work properly:
ddply (df, level (. (B)), conversion, D = sample (C))
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,
ave
transform (df, d = ave (c, b, fun = function (b) sample (b, replace = TRUE))
No comments:
Post a Comment