Re: [R] Recoding multiple columns consistently
Ron Crump wrote: Hi, I have a dataframe that contains pedigree information; that is individual, sire and dam identities as separate columns. It also has date of birth. These identifiers are not numeric, or not sequential. Obviously, an identifier can appear in one or two columns, depending on whether it was a parent or not. These should be consistent. Not all identifiers appear in the individual column - it is possible for a parent not to have its own record if its parents were not known. Missing parental (sire and/or dam) identifiers can occur. I need to export the data for use in another program that requires the pedigree to be coded as integers, increasing with date of birth (therefore sire and dam always have lower identifiers than their offspring) and with missing values coded as 0. How would I go about doing this? And a second, simpler related question, if I have a column with n different values (may be strings or non-sequential integers) identifying levels (possibly with repeated occurences), how can I recode them to be sequential from 1 to n? rank(x, ties.method=first) For the question above you can do as follows, for example: order() identifiers by date, make them unique() and assign them to a new levels object. Then make them ordered factors: factor(some_column, levels=levels, ordered = TRUE) and then as.numeric(factor_object) is what you are going to get. Uwe Ligges I can solve both problems in fortran, so could use loops to do it in R, but feel there should be quicker, more elegant, more R solution. Thanks for your help. Ron. __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.
Re: [R] Recoding multiple columns consistently
Ron Crump wrote: Hi, I have a dataframe that contains pedigree information; that is individual, sire and dam identities as separate columns. It also has date of birth. These identifiers are not numeric, or not sequential. Obviously, an identifier can appear in one or two columns, depending on whether it was a parent or not. These should be consistent. Not all identifiers appear in the individual column - it is possible for a parent not to have its own record if its parents were not known. Missing parental (sire and/or dam) identifiers can occur. I need to export the data for use in another program that requires the pedigree to be coded as integers, increasing with date of birth (therefore sire and dam always have lower identifiers than their offspring) and with missing values coded as 0. How would I go about doing this? Hi Ron, Without the genealogical coding system for the output, I can only make a guess. It seems as though you are going from a series of records for which the index is the individual, followed by fields containing sire, dam and date of birth (perhaps not in that order). I think you want to transform this into a network (maybe hierarchical unless consanguinuity intervenes) with individuals coded as positive integers (and maybe some or all of the original information attached to those identifiers). At a guess, I would recode the birthdates as integers, preserving the order and including a rule for breaking ties. Assuming that you want an inverted tree for each individual, construct a linked list beginning with the individual with two pointers to the parents (their integer identifiers). Each parent has two links pointing to their parents, and so on. Whenever a pointer is zero, the linking stops. I don't know whether this can be represented in any of the tree diagrams in R, but it certainly could be coded. I think a bit more information for non-genealogists about the formats might elicit a more specific answer. And a second, simpler related question, if I have a column with n different values (may be strings or non-sequential integers) identifying levels (possibly with repeated occurences), how can I recode them to be sequential from 1 to n? I can solve both problems in fortran, so could use loops to do it in R, but feel there should be quicker, more elegant, more R solution. sounds like sort Jim __ R-help@stat.math.ethz.ch 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] Recoding multiple columns consistently
Hi, I have a dataframe that contains pedigree information; that is individual, sire and dam identities as separate columns. It also has date of birth. These identifiers are not numeric, or not sequential. Obviously, an identifier can appear in one or two columns, depending on whether it was a parent or not. These should be consistent. Not all identifiers appear in the individual column - it is possible for a parent not to have its own record if its parents were not known. Missing parental (sire and/or dam) identifiers can occur. I need to export the data for use in another program that requires the pedigree to be coded as integers, increasing with date of birth (therefore sire and dam always have lower identifiers than their offspring) and with missing values coded as 0. How would I go about doing this? And a second, simpler related question, if I have a column with n different values (may be strings or non-sequential integers) identifying levels (possibly with repeated occurences), how can I recode them to be sequential from 1 to n? I can solve both problems in fortran, so could use loops to do it in R, but feel there should be quicker, more elegant, more R solution. Thanks for your help. Ron. __ R-help@stat.math.ethz.ch 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] Recoding
I want to do some recoding of variables: code Age into groups and recode a factor into a smaller number of levels. There are a couple of options for recode functions, in the car package and in memisc, and I think in gmisc. Does anyone have any opinions on the the easiest, most reliable approach for these problems? David Scott _ David Scott Department of Statistics, Tamaki Campus The University of Auckland, PB 92019 Auckland 1142,NEW ZEALAND Phone: +64 9 373 7599 ext 86830 Fax: +64 9 373 7000 Email: [EMAIL PROTECTED] Graduate Officer, Department of Statistics Director of Consulting, Department of Statistics __ R-help@stat.math.ethz.ch 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.
Re: [R] Recoding
I've used the one in car with no problems. For grouping a continuous variable you might also consider cut. On 11/06/07, David Scott [EMAIL PROTECTED] wrote: I want to do some recoding of variables: code Age into groups and recode a factor into a smaller number of levels. There are a couple of options for recode functions, in the car package and in memisc, and I think in gmisc. Does anyone have any opinions on the the easiest, most reliable approach for these problems? David Scott _ David Scott Department of Statistics, Tamaki Campus The University of Auckland, PB 92019 Auckland 1142,NEW ZEALAND Phone: +64 9 373 7599 ext 86830 Fax: +64 9 373 7000 Email: [EMAIL PROTECTED] Graduate Officer, Department of Statistics Director of Consulting, Department of Statistics __ R-help@stat.math.ethz.ch 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. -- = David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP __ R-help@stat.math.ethz.ch 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] recoding question
Hi R-users, I have a data frame like this: photographer category picture points Hannu kalat limamikko 1 Teemu kalat verkkovaja 3 Hate kalat munat puoliks padassa 6 Hannu kalat isokala 8 Teemu kasvit, sienet ja muut eliöt harppi 2 Hate kasvit, sienet ja muut eliöt pyynikki 2 Petteri kasvit, sienet ja muut eliöt harmaaleppä 5 Lauri kasvit, sienet ja muut eliöt lumipuu 9 Teemu linnut kainostelua 1 Petteri linnut viileä harakka 2 Lauri linnut hipö 3 Teemu linnut pinnalla 5 Lassi linnut elämän viiva 7 Lassi nisäkkäät pedot 1 Teemu nisäkkäät Homo sapiens angelus 3 Hate nisäkkäät madekoukkujen suojelupyhimys 3 Lassi nisäkkäät portsan kundi 3 Hannu nisäkkäät maukasta marmeladia 8 Teemu reissut puikot 1 Lassi reissut ajatelkaa, jos Häntä ei olisikaan 2 Lauri reissut kökar 3 Hate reissut matka aikaan joka ei enää palaa 3 Hannu reissut skrinnareita 4 Lauri reissut jääpuut 5 Hannu selkärangattomat kaapin alta löytynyt 2 Hate selkärangattomat vailla armeerausta 2 Teemu selkärangattomat portinvartija 6 Hannu selkärangattomat kaapin alta löytynyt2 8 I want to make a new column named as final_points to the data frame that has numbers following this kind of logic: the highest number in column points in each category will receive a number 3 into new column final_points. The second highest number will receive a number 1 into final_points column. Others will receive a 0. Here is how it should look like after the code has been run: photographer category picture points final_points Hannu kalat limamikko 1 0 Teemu kalat verkkovaja 3 0 Hate kalat munat puoliks padassa 6 1 Hannu kalat isokala 8 3 Teemu kasvit, sienet ja muut eliöt harppi 2 0 Hate kasvit, sienet ja muut eliöt pyynikki 2 0 Petteri kasvit, sienet ja muut eliöt harmaaleppä 5 1 Lauri kasvit, sienet ja muut eliöt lumipuu 9 3 Teemu linnut kainostelua 1 0 Petteri linnut viileä harakka 2 0 Lauri linnut hipö 3 0 Teemu linnut pinnalla 5 1 Lassi linnut elämän viiva 7 3 Lassi nisäkkäät pedot 1 0 Teemu nisäkkäät Homo sapiens angelus 3 1 Hate nisäkkäät madekoukkujen suojelupyhimys 3 1 Lassi nisäkkäät portsan kundi 3 1 Hannu nisäkkäät maukasta marmeladia 8 3 Teemu reissut puikot 1 0 Lassi reissut ajatelkaa, jos Häntä ei olisikaan 2 0 Lauri reissut kökar 3 0 Hate reissut matka aikaan joka ei enää palaa 3 0 Hannu reissut skrinnareita 4 1 Lauri reissut jääpuut 5 3 Hannu selkärangattomat kaapin alta löytynyt 2 0 Hate selkärangattomat vailla armeerausta 2 0 Teemu selkärangattomat portinvartija 6 1 Hannu selkärangattomat kaapin alta löytynyt2 8 3 How do I do this with R? -Lauri [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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.
Re: [R] recoding question
Hi, I suppose you wouldn't mind rearranging rows in your dataframe. So just use order() to sort your dataframe according to category (first) and points (second). Now add a new column with values (3,2,1,0,...,0,3,2,1,0...etc.) You can construct such vector using by() or a simple for-loop. There will always be rep(0, n_i-3) if n_i is the number of observatons in category i. If you do mind rearranging rows, keep the original permutation stored elsewhere a use order again in the end to restoring it back. Petr Lauri Nikkinen napsal(a): Hi R-users, I have a data frame like this: photographer category picture points Hannu kalat limamikko 1 Teemu kalat verkkovaja 3 Hate kalat munat puoliks padassa 6 Hannu kalat isokala 8 Teemu kasvit, sienet ja muut eliöt harppi 2 Hate kasvit, sienet ja muut eliöt pyynikki 2 Petteri kasvit, sienet ja muut eliöt harmaaleppä 5 Lauri kasvit, sienet ja muut eliöt lumipuu 9 Teemu linnut kainostelua 1 Petteri linnut viileä harakka 2 Lauri linnut hipö 3 Teemu linnut pinnalla 5 Lassi linnut elämän viiva 7 Lassi nisäkkäät pedot 1 Teemu nisäkkäät Homo sapiens angelus 3 Hate nisäkkäät madekoukkujen suojelupyhimys 3 Lassi nisäkkäät portsan kundi 3 Hannu nisäkkäät maukasta marmeladia 8 Teemu reissut puikot 1 Lassi reissut ajatelkaa, jos Häntä ei olisikaan 2 Lauri reissut kökar 3 Hate reissut matka aikaan joka ei enää palaa 3 Hannu reissut skrinnareita 4 Lauri reissut jääpuut 5 Hannu selkärangattomat kaapin alta löytynyt 2 Hate selkärangattomat vailla armeerausta 2 Teemu selkärangattomat portinvartija 6 Hannu selkärangattomat kaapin alta löytynyt2 8 I want to make a new column named as final_points to the data frame that has numbers following this kind of logic: the highest number in column points in each category will receive a number 3 into new column final_points. The second highest number will receive a number 1 into final_points column. Others will receive a 0. Here is how it should look like after the code has been run: photographer category picture points final_points Hannu kalat limamikko 1 0 Teemu kalat verkkovaja 3 0 Hate kalat munat puoliks padassa 6 1 Hannu kalat isokala 8 3 Teemu kasvit, sienet ja muut eliöt harppi 2 0 Hate kasvit, sienet ja muut eliöt pyynikki 2 0 Petteri kasvit, sienet ja muut eliöt harmaaleppä 5 1 Lauri kasvit, sienet ja muut eliöt lumipuu 9 3 Teemu linnut kainostelua 1 0 Petteri linnut viileä harakka 2 0 Lauri linnut hipö 3 0 Teemu linnut pinnalla 5 1 Lassi linnut elämän viiva 7 3 Lassi nisäkkäät pedot 1 0 Teemu nisäkkäät Homo sapiens angelus 3 1 Hate nisäkkäät madekoukkujen suojelupyhimys 3 1 Lassi nisäkkäät portsan kundi 3 1 Hannu nisäkkäät maukasta marmeladia 8 3 Teemu reissut puikot 1 0 Lassi reissut ajatelkaa, jos Häntä ei olisikaan 2 0 Lauri reissut kökar 3 0 Hate reissut matka aikaan joka ei enää palaa 3 0 Hannu reissut skrinnareita 4 1 Lauri reissut jääpuut 5 3 Hannu selkärangattomat kaapin alta löytynyt 2 0 Hate selkärangattomat vailla armeerausta 2 0 Teemu selkärangattomat portinvartija 6 1 Hannu selkärangattomat kaapin alta löytynyt2 8 3 How do I do this with R? -Lauri [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. -- Petr Klasterecky Dept. of Probability and Statistics Charles University in Prague Czech Republic __ R-help@stat.math.ethz.ch 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] Recoding categorical variables
I want to recode an integer-valued variable y so that its values become 1:length(y). I can do this using a loop but maybe someone can suggest code without a loop. My code is this: y - round(20*runif(25)) table(y) suy - sort(unique(y)) m - length(suy) z - y + max(suy) for(i in 1:m) z[y==suy[i]] - i rbind(y,z) (the recoded y is stored in z) Murray Jorgensen -- Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html Department of Statistics, University of Waikato, Hamilton, New Zealand Email: [EMAIL PROTECTED]Fax 7 838 4155 Phone +64 7 838 4773 wkHome +64 7 825 0441Mobile 021 1395 862 __ R-help@stat.math.ethz.ch 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.
Re: [R] Recoding categorical variables
Dear Murray, How about as.numeric(factor(y)) ? I hope this helps, John John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Murray Jorgensen Sent: Wednesday, October 25, 2006 7:13 PM To: r-help@stat.math.ethz.ch Subject: [R] Recoding categorical variables I want to recode an integer-valued variable y so that its values become 1:length(y). I can do this using a loop but maybe someone can suggest code without a loop. My code is this: y - round(20*runif(25)) table(y) suy - sort(unique(y)) m - length(suy) z - y + max(suy) for(i in 1:m) z[y==suy[i]] - i rbind(y,z) (the recoded y is stored in z) Murray Jorgensen -- Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html Department of Statistics, University of Waikato, Hamilton, New Zealand Email: [EMAIL PROTECTED]Fax 7 838 4155 Phone +64 7 838 4773 wkHome +64 7 825 0441Mobile 021 1395 862 __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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.
Re: [R] Recoding categorical variables
Thanks John! Deepayan Sarkar also suggested this. I don't really expect to see any better solution. Murray John Fox wrote: Dear Murray, How about as.numeric(factor(y)) ? I hope this helps, John John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Murray Jorgensen Sent: Wednesday, October 25, 2006 7:13 PM To: r-help@stat.math.ethz.ch Subject: [R] Recoding categorical variables I want to recode an integer-valued variable y so that its values become 1:length(y). I can do this using a loop but maybe someone can suggest code without a loop. My code is this: y - round(20*runif(25)) table(y) suy - sort(unique(y)) m - length(suy) z - y + max(suy) for(i in 1:m) z[y==suy[i]] - i rbind(y,z) (the recoded y is stored in z) Murray Jorgensen -- Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html Department of Statistics, University of Waikato, Hamilton, New Zealand Email: [EMAIL PROTECTED]Fax 7 838 4155 Phone +64 7 838 4773 wkHome +64 7 825 0441Mobile 021 1395 862 __ R-help@stat.math.ethz.ch 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. -- Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html Department of Statistics, University of Waikato, Hamilton, New Zealand Email: [EMAIL PROTECTED]Fax 7 838 4155 Phone +64 7 838 4773 wkHome +64 7 825 0441Mobile 021 1395 862 __ R-help@stat.math.ethz.ch 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] recoding large number of categories (select in SAS)
Hi, I have data on stomach contents. Possible prey species are in the hundreds, so a list of prey codes has been in used in many labs doing this kind of work. When comes time to do analyses on these data one often wants to regroup prey in broader categories, especially for rare prey. In SAS you can nest a large number of if-else, or do this more cleanly with select like this: select; when (149 = prey =150) preyGr= 150; when (186 = prey = 187) preyGr= 187; when (prey= 438) preyGr= 438; when (prey= 430) preyGr= 430; when (prey= 436) preyGr= 436; when (prey= 431) preyGr= 431; when (prey= 451) preyGr= 451; when (prey= 461) preyGr= 461; when (prey= 478) preyGr= 478; when (prey= 572) preyGr= 572; when (692 = prey = 695 ) preyGr= 692; when (808 = prey = 826, 830 = prey = 832 ) preyGr= 808; when (997 = prey = 998, 792 = prey = 796) preyGr= 792; when (882 = prey = 909) preyGr= 882; when (prey in (999, 125, 994)) preyGr= 9994; otherwise preyGr= 1; end; *select; The number of transformations is usually much larger than this short example. What is the best way of doing this in R? Sincerely, Denis Chabot __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] recoding large number of categories (select in SAS)
Does ?cut answers to your question? Best, Philippe Grosjean ..°})) ) ) ) ) ) ( ( ( ( (Prof. Philippe Grosjean ) ) ) ) ) ( ( ( ( (Numerical Ecology of Aquatic Systems ) ) ) ) ) Mons-Hainaut University, Pentagone (3D08) ( ( ( ( (Academie Universitaire Wallonie-Bruxelles ) ) ) ) ) 8, av du Champ de Mars, 7000 Mons, Belgium ( ( ( ( ( ) ) ) ) ) phone: + 32.65.37.34.97, fax: + 32.65.37.30.54 ( ( ( ( (email: [EMAIL PROTECTED] ) ) ) ) ) ( ( ( ( (web: http://www.umh.ac.be/~econum ) ) ) ) ) http://www.sciviews.org ( ( ( ( ( .. Denis Chabot wrote: Hi, I have data on stomach contents. Possible prey species are in the hundreds, so a list of prey codes has been in used in many labs doing this kind of work. When comes time to do analyses on these data one often wants to regroup prey in broader categories, especially for rare prey. In SAS you can nest a large number of if-else, or do this more cleanly with select like this: select; when (149 = prey =150) preyGr= 150; when (186 = prey = 187) preyGr= 187; when (prey= 438) preyGr= 438; when (prey= 430) preyGr= 430; when (prey= 436) preyGr= 436; when (prey= 431) preyGr= 431; when (prey= 451) preyGr= 451; when (prey= 461) preyGr= 461; when (prey= 478) preyGr= 478; when (prey= 572) preyGr= 572; when (692 = prey = 695 ) preyGr= 692; when (808 = prey = 826, 830 = prey = 832 ) preyGr= 808; when (997 = prey = 998, 792 = prey = 796) preyGr= 792; when (882 = prey = 909) preyGr= 882; when (prey in (999, 125, 994)) preyGr= 9994; otherwise preyGr= 1; end; *select; The number of transformations is usually much larger than this short example. What is the best way of doing this in R? Sincerely, Denis Chabot __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] recoding large number of categories (select in SAS)
Here is a way of doing it by setting up a matrix of values to test against. Easier than writing all the 'select' statements. x.trans - matrix(c( # translation matrix; first column is min, second is max, + 149, 150, 150, # and third is the value to be returned + 186, 187, 187, + 438, 438, 438, + 430, 430, 430, + 808, 826, 808, + 830, 832, 808, + 997, 998, 792, + 792, 796, 792), ncol=3, byrow=T) colnames(x.trans) - c('min', 'max', 'value') x.default - # default/nomatch value x.test - c(150, 149, 148, 438, 997, 791, 795, 810, 820, 834) # test data # # this function will test each value and if between the min/max, return 3 column # newValues - sapply(x.test, function(x){ + .value - x.trans[(x = x.trans[,'min']) (x = x.trans[,'max']),'value'] + if (length(.value) == 0) .value - x.default# on no match, take default + .value[1] # return first value if multiple matches + }) newValues [1] 150 150 438 792 792 808 808 __ James HoltmanWhat is the problem you are trying to solve? Executive Technical Consultant -- Office of Technology, Convergys [EMAIL PROTECTED] +1 (513) 723-2929 Denis Chabot [EMAIL PROTECTED]To: r-help@stat.math.ethz.ch .netcc: Sent by: Subject: [R] recoding large number of categories (select in SAS) [EMAIL PROTECTED] ath.ethz.ch 01/19/2005 08:56 AM Hi, I have data on stomach contents. Possible prey species are in the hundreds, so a list of prey codes has been in used in many labs doing this kind of work. When comes time to do analyses on these data one often wants to regroup prey in broader categories, especially for rare prey. In SAS you can nest a large number of if-else, or do this more cleanly with select like this: select; when (149 = prey =150) preyGr= 150; when (186 = prey = 187) preyGr= 187; when (prey= 438) preyGr= 438; when (prey= 430) preyGr= 430; when (prey= 436) preyGr= 436; when (prey= 431) preyGr= 431; when (prey= 451) preyGr= 451; when (prey= 461) preyGr= 461; when (prey= 478) preyGr= 478; when (prey= 572) preyGr= 572; when (692 = prey = 695 ) preyGr= 692; when (808 = prey = 826, 830 = prey = 832 ) preyGr= 808; when (997 = prey = 998, 792 = prey = 796) preyGr= 792; when (882 = prey = 909) preyGr= 882; when (prey in (999, 125, 994)) preyGr= 9994; otherwise preyGr= 1; end; *select; The number of transformations is usually much larger than this short example. What is the best way of doing this in R? Sincerely, Denis Chabot __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] recoding large number of categories (select in SAS)
Philippe Grosjean [EMAIL PROTECTED] writes: Does ?cut answers to your question? That's one way, but it tends to get messy to get the names right. You might consider using the rather little-known variant of levels assignment: preyGR - prey # or factor(prey) if it wasn't one already levels(preyGR) - list(150=149:150, 187=187:188, 438=438, [...] 9994=c(999,125,994), 1=NA) preyGR[is.na(preyGR) !is.na(prey)] - 1 This would be roughly as clean as the SAS way, only the otherwise case got a bit tricky. Best, Philippe Grosjean ..°})) ) ) ) ) ) ( ( ( ( (Prof. Philippe Grosjean ) ) ) ) ) ( ( ( ( (Numerical Ecology of Aquatic Systems ) ) ) ) ) Mons-Hainaut University, Pentagone (3D08) ( ( ( ( (Academie Universitaire Wallonie-Bruxelles ) ) ) ) ) 8, av du Champ de Mars, 7000 Mons, Belgium ( ( ( ( ( ) ) ) ) ) phone: + 32.65.37.34.97, fax: + 32.65.37.30.54 ( ( ( ( (email: [EMAIL PROTECTED] ) ) ) ) ) ( ( ( ( (web: http://www.umh.ac.be/~econum ) ) ) ) ) http://www.sciviews.org ( ( ( ( ( .. Denis Chabot wrote: Hi, I have data on stomach contents. Possible prey species are in the hundreds, so a list of prey codes has been in used in many labs doing this kind of work. When comes time to do analyses on these data one often wants to regroup prey in broader categories, especially for rare prey. In SAS you can nest a large number of if-else, or do this more cleanly with select like this: select; when (149 = prey =150) preyGr= 150; when (186 = prey = 187) preyGr= 187; when (prey= 438) preyGr= 438; when (prey= 430) preyGr= 430; when (prey= 436) preyGr= 436; when (prey= 431) preyGr= 431; when (prey= 451) preyGr= 451; when (prey= 461) preyGr= 461; when (prey= 478) preyGr= 478; when (prey= 572) preyGr= 572; when (692 = prey = 695 ) preyGr= 692; when (808 = prey = 826, 830 = prey = 832 ) preyGr= 808; when (997 = prey = 998, 792 = prey = 796) preyGr= 792; when (882 = prey = 909) preyGr= 882; when (prey in (999, 125, 994)) preyGr= 9994; otherwise preyGr= 1; end; *select; The number of transformations is usually much larger than this short example. What is the best way of doing this in R? Sincerely, Denis Chabot __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- O__ Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] recoding large number of categories (select in SAS)
Dear Peter et al., The recode() function in the car package will also do this kind of thing, will work even when the ranges include non-integers, and supports an else= construction. Regards, John John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Peter Dalgaard Sent: Wednesday, January 19, 2005 10:52 AM To: Philippe Grosjean Cc: r-help@stat.math.ethz.ch; Denis Chabot Subject: Re: [R] recoding large number of categories (select in SAS) Philippe Grosjean [EMAIL PROTECTED] writes: Does ?cut answers to your question? That's one way, but it tends to get messy to get the names right. You might consider using the rather little-known variant of levels assignment: preyGR - prey # or factor(prey) if it wasn't one already levels(preyGR) - list(150=149:150, 187=187:188, 438=438, [...] 9994=c(999,125,994), 1=NA) preyGR[is.na(preyGR) !is.na(prey)] - 1 This would be roughly as clean as the SAS way, only the otherwise case got a bit tricky. Best, Philippe Grosjean ..°})) ) ) ) ) ) ( ( ( ( (Prof. Philippe Grosjean ) ) ) ) ) ( ( ( ( (Numerical Ecology of Aquatic Systems ) ) ) ) ) Mons-Hainaut University, Pentagone (3D08) ( ( ( ( (Academie Universitaire Wallonie-Bruxelles ) ) ) ) ) 8, av du Champ de Mars, 7000 Mons, Belgium ( ( ( ( ( ) ) ) ) ) phone: + 32.65.37.34.97, fax: + 32.65.37.30.54 ( ( ( ( (email: [EMAIL PROTECTED] ) ) ) ) ) ( ( ( ( (web: http://www.umh.ac.be/~econum ) ) ) ) ) http://www.sciviews.org ( ( ( ( ( .. Denis Chabot wrote: Hi, I have data on stomach contents. Possible prey species are in the hundreds, so a list of prey codes has been in used in many labs doing this kind of work. When comes time to do analyses on these data one often wants to regroup prey in broader categories, especially for rare prey. In SAS you can nest a large number of if-else, or do this more cleanly with select like this: select; when (149 = prey =150) preyGr= 150; when (186 = prey = 187) preyGr= 187; when (prey= 438) preyGr= 438; when (prey= 430) preyGr= 430; when (prey= 436) preyGr= 436; when (prey= 431) preyGr= 431; when (prey= 451) preyGr= 451; when (prey= 461) preyGr= 461; when (prey= 478) preyGr= 478; when (prey= 572) preyGr= 572; when (692 = prey = 695 ) preyGr= 692; when (808 = prey = 826, 830 = prey = 832 ) preyGr= 808; when (997 = prey = 998, 792 = prey = 796) preyGr= 792; when (882 = prey = 909) preyGr= 882; when (prey in (999, 125, 994)) preyGr= 9994; otherwise preyGr= 1; end; *select; The number of transformations is usually much larger than this short example. What is the best way of doing this in R? Sincerely, Denis Chabot __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- O__ Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Recoding factors
I'm having a bit of trouble recoding factors in a fields. I have a field which has the factors Singleton, Twin and Triplet. I want to recode the field to have only the factors Singleton and Multiple. Any advice? Cheers, Neil __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Recoding factors
Neil Leonard wrote: I'm having a bit of trouble recoding factors in a fields. I have a field which has the factors Singleton, Twin and Triplet. I want to recode the field to have only the factors Singleton and Multiple. Any advice? Depends on what you are going to do with the Twin ... Uwe Ligges Cheers, Neil __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Recoding factors
I want Twin and Triplet to both be recoded as Multiple Neil On 18/10/2004, at 7:43 PM, Uwe Ligges wrote: Neil Leonard wrote: I'm having a bit of trouble recoding factors in a fields. I have a field which has the factors Singleton, Twin and Triplet. I want to recode the field to have only the factors Singleton and Multiple. Any advice? Depends on what you are going to do with the Twin ... Uwe Ligges Cheers, Neil __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Recoding factors
On Mon, 18 Oct 2004, Uwe Ligges wrote: Neil Leonard wrote: I'm having a bit of trouble recoding factors in a fields. I have a field which has the factors Singleton, Twin and Triplet. I want to recode the field to have only the factors Singleton and Multiple. Any advice? Depends on what you are going to do with the Twin ... ?levels should help, though. x - factor(c(Singleton, Twin, Triplet)) levels(x) - c(Singleton, Multiple, Multiple) x [1] Singleton Multiple Multiple Levels: Singleton Multiple might be what is intended. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Recoding factors
Yes that's it. Thanks Neil On 18/10/2004, at 7:57 PM, Prof Brian Ripley wrote: On Mon, 18 Oct 2004, Uwe Ligges wrote: Neil Leonard wrote: I'm having a bit of trouble recoding factors in a fields. I have a field which has the factors Singleton, Twin and Triplet. I want to recode the field to have only the factors Singleton and Multiple. Any advice? Depends on what you are going to do with the Twin ... ?levels should help, though. x - factor(c(Singleton, Twin, Triplet)) levels(x) - c(Singleton, Multiple, Multiple) x [1] Singleton Multiple Multiple Levels: Singleton Multiple might be what is intended. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Recoding factors
See ?levels. I think you want something like the following: levels(myfact) - c(Singleton, Multiple, Multiple) Neil Leonard wrote: I'm having a bit of trouble recoding factors in a fields. I have a field which has the factors Singleton, Twin and Triplet. I want to recode the field to have only the factors Singleton and Multiple. Any advice? -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Recoding scores of negatively worded item
Hi, I'm new to R so please fogive if I write someting silly ... I need to recode a series of responses from a number of questionnaires. The data is read via ODBC from a database where all responses are coded as tables of the form (id, question, score). After dealing with recoding of missing values, I need to invert the scores of some questionnaire's item in the form x - (n - x) where n is the range of allowed responses + 1, e.g. if score can range from 1 to 4 n would by 5. I am using R version 1.9.1 and 1.8.1 with identical outcome. If from the R interpreter I write ces[is.element(ces$question,c(1,3,5),]$score - 5 - ces[is.element(ces$question,c(1,3,5),]$score the system correctly recode the scores for questions 1,3,5 on the table ces. If the same expression is processed as part of a script I get a syntax error. My question is a) is this the best way to recode scores? (I did look at the package car but I did not see how to perform a conditional recoding, nor was obvious how to to operations on scores) b) why there is a different behavior from the command line and from a script? c) how to solve the problem? (As the amount of data is large I need to do everything via scripts) Thank you for your attention. -- Saluti, Antonio Prioglio -- We are what we repeatedly do. Excellence, then, is not an act, but a habit. Aristoteles /\ \ /ASCII RIBBON CAMPAIGN - AGAINST HTML MAIL X - AGAINST MS ATTACHMENTS / \ http://www.gnu.org/philosophy/no-word-attachments.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Recoding scores of negatively worded item
On 07/03/04 16:45, [EMAIL PROTECTED] wrote: Hi, I'm new to R so please fogive if I write someting silly ... I need to recode a series of responses from a number of questionnaires. The data is read via ODBC from a database where all responses are coded as tables of the form (id, question, score). After dealing with recoding of missing values, I need to invert the scores of some questionnaire's item in the form x - (n - x) where n is the range of allowed responses + 1, e.g. if score can range from 1 to 4 n would by 5. I am using R version 1.9.1 and 1.8.1 with identical outcome. If from the R interpreter I write ces[is.element(ces$question,c(1,3,5),]$score - 5 - ces[is.element(ces$question,c(1,3,5),]$score the system correctly recode the scores for questions 1,3,5 on the table ces. If the same expression is processed as part of a script I get a syntax error. My question is a) is this the best way to recode scores? (I did look at the package car but I did not see how to perform a conditional recoding, nor was obvious how to to operations on scores) Other (but similar) ways you might try are: ces[ces$question %in% c(1,3,5),]$score - 5-ces[ces$question %in% c(1,3,5),]$score ces$score - ifelse(ces$question %in% c(1,3,5),5-ces$score,ces$score) b) why there is a different behavior from the command line and from a script? Could the problem be on the line before the one that gives the error? Jon -- Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page:http://www.sas.upenn.edu/~baron R search page:http://finzi.psych.upenn.edu/ __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Recoding scores of negatively worded item
Jonathan Baron baron at psych.upenn.edu writes: : ces$score - ifelse(ces$question %in% c(1,3,5),5-ces$score,ces$score) One minor improvement in readability might be: ces$score - with(ces, ifelse(question %in% c(1,3,5), 5-score, score)) __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Recoding scores of negatively worded item
On Sat, Jul 03, 2004 at 12:17:11PM -0400, Jonathan Baron wrote: b) why there is a different behavior from the command line and from a script? Could the problem be on the line before the one that gives the error? I guess I discovered what was the issue ( I knew it was something silly) In the script I broke the line in two for easier reading forgetting that the left hand side is a valid statement in itself! This is nasty. Thanks again for your help. -- Saluti, Antonio Prioglio -- We are what we repeatedly do. Excellence, then, is not an act, but a habit. Aristoteles /\ \ /ASCII RIBBON CAMPAIGN - AGAINST HTML MAIL X - AGAINST MS ATTACHMENTS / \ http://www.gnu.org/philosophy/no-word-attachments.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] Recoding a multiple response question into a series of 1, 0 variables
Hello, Here is a slightly more sophisticate and fully vectorized, answer. RecodeChoices - function(mat) { # Make sure mat is a matrix (in case it is a data.frame) mat - as.matrix(mat) # Get dimensions of the matrix Dim - dim(mat) Nr - Dim[1] Nc - Dim[2] # Flatten it into a vector, but by row (need to transpose first!) mat - t(mat) dim(mat) - NULL # Offset is a vector of offsets to make locations unique in vector mat # (a solution to avoid loops, see Jonathan Baron's answer) Offset - sort(rep(0:(Nr - 1) * Nc, Nc)) # Initialize a vector of results of the same size with 0's res - rep(0, Nr * Nc) # Now replace locations pointed by (mat + Offset) by 1 in res res[mat + Offset] - 1 # Transform res into a matrix of same size of mat, by row res - matrix(res, nrow = Nr, byrow = TRUE) # Return the result return(res) } # Now your example: A - matrix(c(4, 2, NA, NA, NA, 1, 3, 4, 5, NA, 3, 2, NA, NA, NA), nrow = 3, byrow = TRUE) A RecodeChoices(A) Depending on the use you make of this, it is perhaps preferable to recode it as a boolean (as.numeric() would give you the c(1, 0) as above easily). To do this, just replace: res - rep(0, Nr * Nc) by res - rep(FALSE, Nr * Nc) and: res[mat + Offset] - TRUE You may also consider to make it factors... and to finalize this function, you should add code to collect row and column names from mat and apply them to res, and perhaps transforn res into a data.frame if mat was a data.frame itself. Best, Philippe Grosjean ...?})) ) ) ) ) ) ( ( ( ( ( Prof. Philippe Grosjean \ ___ ) \/ECO\ ( Numerical Ecology of Aquatic Systems /\___/ ) Mons-Hainaut University, Pentagone / ___ /( 8, Av. du Champ de Mars, 7000 Mons, Belgium /NUM\/ ) \___/\ ( phone: + 32.65.37.34.97, fax: + 32.65.37.33.12 \ ) email: [EMAIL PROTECTED] ) ) ) ) ) SciViews project coordinator (http://www.sciviews.org) ( ( ( ( ( ... -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Jonathan Baron Sent: Tuesday, 08 June, 2004 04:45 To: Greg Blevins Cc: R-Help Subject: Re: [R] Recoding a multiple response question into a series of 1,0 variables On 06/07/04 21:28, Greg Blevins wrote: Hello R folks. 1) The question that generated the data, which I call Qx: Which of the following 5 items have you performed in the past month? (multipe response) 2) How the data is coded in my current dataframe: The first item that a person selected is coded under a field called Qxfirst; the second selected under Qxsecond, etc. For the first Person, the NAs mean that that person only selected two of the five items. Hypothetical data is shown QxfirstQxsecondQxthirdQxfourthQxfifth Person142NANANA Person2134 5 NA Person332NANANA 3) How I want the data to be be coded: I want each field to be one of the five items and I want each field to contain a 1 or 0 code--1 if they mentioned the item, 0 otherwise. Given the above data, the new fields would look as follows: Item1Item2Item3Item4 Item5 Person101 01 0 Person210 11 1 Person301 10 0 Here is an idea: X - c(4,5,NA,NA,NA) # one row Y - rep(NA,5) # an empty row Y[X] - 1 Y is now NA NA NA 1 1 which is what you want. So you need to do this on each row and then convert the NAs to 0s. So first create an empty data frame, the same size as your original one X, like my Y. Callit Y. Then a loop? (I can't think of a better way just now, like with mapply.) for (i in [whatever]) Y[i][X[i]] - 1 (Not tested.) Jon -- Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page:http://www.sas.upenn.edu/~baron R search page:http://finzi.psych.upenn.edu/ __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Recoding a multiple response question into a series of 1, 0 variables
Hello R folks. 1) The question that generated the data, which I call Qx: Which of the following 5 items have you performed in the past month? (multipe response) 2) How the data is coded in my current dataframe: The first item that a person selected is coded under a field called Qxfirst; the second selected under Qxsecond, etc. For the first Person, the NAs mean that that person only selected two of the five items. Hypothetical data is shown QxfirstQxsecondQxthirdQxfourthQxfifth Person142NANANA Person2134 5 NA Person332NANANA 3) How I want the data to be be coded: I want each field to be one of the five items and I want each field to contain a 1 or 0 code--1 if they mentioned the item, 0 otherwise. Given the above data, the new fields would look as follows: Item1Item2Item3Item4Item5 Person101 01 0 Person210 11 1 Person301 10 0 I know how to do this using brute force (by writing bunch of ifelse statements), but given I have quite a lot of data in the above format, I was hoping a function would streamline this--I have tried to create a function for this, but my efforts to date have turned up junk. Thanks Greg Blevins The Market Solutions Group [[alternative HTML version deleted]] __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Recoding a multiple response question into a series of 1, 0 variables
On 06/07/04 21:28, Greg Blevins wrote: Hello R folks. 1) The question that generated the data, which I call Qx: Which of the following 5 items have you performed in the past month? (multipe response) 2) How the data is coded in my current dataframe: The first item that a person selected is coded under a field called Qxfirst; the second selected under Qxsecond, etc. For the first Person, the NAs mean that that person only selected two of the five items. Hypothetical data is shown QxfirstQxsecondQxthirdQxfourthQxfifth Person142NANANA Person2134 5 NA Person332NANANA 3) How I want the data to be be coded: I want each field to be one of the five items and I want each field to contain a 1 or 0 code--1 if they mentioned the item, 0 otherwise. Given the above data, the new fields would look as follows: Item1Item2Item3Item4Item5 Person101 01 0 Person210 11 1 Person301 10 0 Here is an idea: X - c(4,5,NA,NA,NA) # one row Y - rep(NA,5) # an empty row Y[X] - 1 Y is now NA NA NA 1 1 which is what you want. So you need to do this on each row and then convert the NAs to 0s. So first create an empty data frame, the same size as your original one X, like my Y. Callit Y. Then a loop? (I can't think of a better way just now, like with mapply.) for (i in [whatever]) Y[i][X[i]] - 1 (Not tested.) Jon -- Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page:http://www.sas.upenn.edu/~baron R search page:http://finzi.psych.upenn.edu/ __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Recoding a multiple response question into a series of 1, 0 variables
On 06/07/04 22:45, Jonathan Baron should have written: for (i in [whatever]) Y[i,][X[i,]] - 1 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] recoding variables
Hi, I was hoping someone could help me. I am a graduate student new to using R, and I'm trying to figure out how to recode a continuous variable to make it into an ordinal variable with 3 categories. I literally have no idea how to proceed--could anyone possibly advise me? Please copy me on any responses, as I have just subscribed to the R-help email list but don't know whether the subscription has gone through yet. Thanks so much- Shayna __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] recoding variables
Shayna Strom wrote: Hi, I was hoping someone could help me. I am a graduate student new to using R, and I'm trying to figure out how to recode a continuous variable to make it into an ordinal variable with 3 categories. I literally have no idea how to proceed--could anyone possibly advise me? Please copy me on any responses, as I have just subscribed to the R-help email list but don't know whether the subscription has gone through yet. Thanks so much- Shayna __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Sorry sent too quickly. Probably what you want is ?cut too. x - runif(30) xcut - cut(x, c(0, 1/3, 2/3, 1)) xord - ordered(xcut) __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] recoding variables
Basically, you want to use cut(). For example, x - 1:20 cut(x, c(0, 8, 15, 20)) This divides x into a factor with levels (0,8], (8,15], and (15,20]. -roger Shayna Strom wrote: Hi, I was hoping someone could help me. I am a graduate student new to using R, and I'm trying to figure out how to recode a continuous variable to make it into an ordinal variable with 3 categories. I literally have no idea how to proceed--could anyone possibly advise me? Please copy me on any responses, as I have just subscribed to the R-help email list but don't know whether the subscription has gone through yet. Thanks so much- Shayna __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] recoding variables
Shayna Strom wrote: Hi, I was hoping someone could help me. I am a graduate student new to using R, and I'm trying to figure out how to recode a continuous variable to make it into an ordinal variable with 3 categories. I literally have no idea how to proceed--could anyone possibly advise me? Please copy me on any responses, as I have just subscribed to the R-help email list but don't know whether the subscription has gone through yet. Thanks so much- Shayna __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html See ?ordered. x - rep(runif(3), 10) xord - ordered(x) __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] recoding variables
(tst - cut(1:11, c(-Inf, 3, 5, Inf))) [1] (-Inf,3] (-Inf,3] (-Inf,3] (3,5](3,5](5,Inf] (5,Inf] (5,Inf] [9] (5,Inf] (5,Inf] (5,Inf] Levels: (-Inf,3] (3,5] (5,Inf] class(tst) [1] factor hope this helps. spencer graves Shayna Strom wrote: Hi, I was hoping someone could help me. I am a graduate student new to using R, and I'm trying to figure out how to recode a continuous variable to make it into an ordinal variable with 3 categories. I literally have no idea how to proceed--could anyone possibly advise me? Please copy me on any responses, as I have just subscribed to the R-help email list but don't know whether the subscription has gone through yet. Thanks so much- Shayna __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] recoding variables
Hi! using R, and I'm trying to figure out how to recode a continuous variable to make it into an ordinal variable with 3 categories. You are looking for cut(): a - runif(15) a [1] 0.19109987 0.78808597 0.78458256 0.31355035 0.02076274 0.82287287 0.75260382 0.82627690 0.14775167 [10] 0.59427620 0.12314764 0.44151537 0.05123785 0.88879744 0.25552054 b = cut(a, breaks=c(0, 0.33, 0.66, 1), labels=c('low', 'medium', 'high')) b [1] lowhigh high lowlowhigh high high low medium lowmedium lowhigh low Levels: low medium high cu Philipp -- Dr. Philipp PagelTel. +49-89-3187-3675 Institute for Bioinformatics / MIPS Fax. +49-89-3187-3585 GSF - National Research Center for Environment and Health Ingolstaedter Landstrasse 1 85764 Neuherberg, Germany http://mips.gsf.de/~pagel __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Recoding problem
Hello I have the following variables, all of which are logicals fmar15 fcoc15 fher15fcrk15fidu15 what I would like is a variable drug15 which equals idu if fidu15 is T; crk if fidu15 is F but fcrk is T, her if fher15 is T but fcrk15 and fidu15 are F and so on What's the best way to do this? Thanks in advance Peter L. Flom, PhD Assistant Director, Statistics and Data Analysis Core Center for Drug Use and HIV Research National Development and Research Institutes 71 W. 23rd St www.peterflom.com New York, NY 10010 (212) 845-4485 (voice) (917) 438-0894 (fax) __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Recoding problem
I have the following variables, all of which are logicals fmar15 fcoc15 fher15fcrk15fidu15 what I would like is a variable drug15 which equals idu if fidu15 is T; crk if fidu15 is F but fcrk is T, her if fher15 is T but fcrk15 and fidu15 are F and so on What's the best way to do this? I don't know about the best way, but if I understand your question, the following achieves what you want: tab - cbind(fidu15, fcrk15, fher15, fcoc15, fmar15) substring(colnames(tab), 2, 4)[apply(tab, 1, match, x = T)] HTH Ray Brownrigg __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help