[R] To transform a vector of qualitatives values into a dataframe of quantitatives values
Hi From the vector X - c(A, A, B, C, B, A, C) I would like to build the Dataframe : data.frame( A=c(1,1,0,0,0,1,0), B=c(0,0,1,0,1,0,0), C=c(0,0,0,1,0,0,1)) Any ideas ? -- Michel ARNAUD Chargé de mission auprès du DRH DGDRD-Drh - TA 174/04 Av Agropolis 34398 Montpellier cedex 5 tel : 04.67.61.75.38 fax : 04.67.61.57.87 port: 06.47.43.55.31 __ 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.
Re: [R] To transform a vector of qualitatives values into a dataframe of quantitatives values
Hi -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Arnaud Michel Sent: Wednesday, December 11, 2013 2:01 PM To: R help Subject: [R] To transform a vector of qualitatives values into a dataframe of quantitatives values Hi From the vector X - c(A, A, B, C, B, A, C) What is A, B and C? If you expect them to be letters, they need to be in parentheses. I would like to build the Dataframe : data.frame( A=c(1,1,0,0,0,1,0), B=c(0,0,1,0,1,0,0), C=c(0,0,0,1,0,0,1)) Any ideas ? X - sample(letters[1:3], 10, replace=T) X [1] c c a b c c a a a a data.frame(A=(X==a), B=(X==b), C=(X==c)) Petr -- Michel ARNAUD Chargé de mission auprès du DRH DGDRD-Drh - TA 174/04 Av Agropolis 34398 Montpellier cedex 5 tel : 04.67.61.75.38 fax : 04.67.61.57.87 port: 06.47.43.55.31 __ 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.
Re: [R] To transform a vector of qualitatives values into a dataframe of quantitatives values
On 13-12-11 8:22 AM, PIKAL Petr wrote: Hi -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Arnaud Michel Sent: Wednesday, December 11, 2013 2:01 PM To: R help Subject: [R] To transform a vector of qualitatives values into a dataframe of quantitatives values Hi From the vector X - c(A, A, B, C, B, A, C) What is A, B and C? If you expect them to be letters, they need to be in parentheses. You mean quotes, not parentheses. I would like to build the Dataframe : data.frame( A=c(1,1,0,0,0,1,0), B=c(0,0,1,0,1,0,0), C=c(0,0,0,1,0,0,1)) Any ideas ? X - sample(letters[1:3], 10, replace=T) X [1] c c a b c c a a a a data.frame(A=(X==a), B=(X==b), C=(X==c)) A simpler way is to use model.matrix. With your example, X - factor(X) m - model.matrix( ~ X - 1) (The names of the columns may need adjusting.) Duncan Murdoch __ 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.
Re: [R] To transform a vector of qualitatives values into a dataframe of quantitatives values
more generally x - c(A, A, B, C, B, A, C) as.data.frame(Map(match , table = unique(x), nomatch = 0, x = list(x))) Andrea On 11 December 2013 14:43, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 13-12-11 8:22 AM, PIKAL Petr wrote: Hi -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Arnaud Michel Sent: Wednesday, December 11, 2013 2:01 PM To: R help Subject: [R] To transform a vector of qualitatives values into a dataframe of quantitatives values Hi From the vector X - c(A, A, B, C, B, A, C) What is A, B and C? If you expect them to be letters, they need to be in parentheses. You mean quotes, not parentheses. I would like to build the Dataframe : data.frame( A=c(1,1,0,0,0,1,0), B=c(0,0,1,0,1,0,0), C=c(0,0,0,1,0,0,1)) Any ideas ? X - sample(letters[1:3], 10, replace=T) X [1] c c a b c c a a a a data.frame(A=(X==a), B=(X==b), C=(X==c)) A simpler way is to use model.matrix. With your example, X - factor(X) m - model.matrix( ~ X - 1) (The names of the columns may need adjusting.) Duncan Murdoch __ 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. [[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] To transform a vector
Dear R Users I have the vector X - c( 6 , 4 ,12 , 3) I would like to build a new vector by to transform it into Y - c(rep(X[1], X[1]), rep(X[2], X[2]), rep(X[3], X[3]), rep(X[4], X[4])) Have you a more elegant answer ? PS : Sorry for this basic question -- Michel ARNAUD Chargé de mission auprès du DRH DGDRD-Drh - TA 174/04 Av Agropolis 34398 Montpellier cedex 5 tel : 04.67.61.75.38 fax : 04.67.61.57.87 port: 06.47.43.55.31 __ 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.
Re: [R] To transform a vector
On 09-12-2013, at 08:04, Arnaud Michel michel.arn...@cirad.fr wrote: Dear R Users I have the vector X - c( 6 , 4 ,12 , 3) I would like to build a new vector by to transform it into Y - c(rep(X[1], X[1]), rep(X[2], X[2]), rep(X[3], X[3]), rep(X[4], X[4])) Have you a more elegant answer ? Have a good read of ?rep. Try this: rep(X,times=X) Berend __ 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.
Re: [R] To transform a vector
Hello, Are you looking for Y - rep(X, X) ? Regards, Pascal On 9 December 2013 16:04, Arnaud Michel michel.arn...@cirad.fr wrote: Dear R Users I have the vector X - c( 6 , 4 ,12 , 3) I would like to build a new vector by to transform it into Y - c(rep(X[1], X[1]), rep(X[2], X[2]), rep(X[3], X[3]), rep(X[4], X[4])) Have you a more elegant answer ? PS : Sorry for this basic question -- Michel ARNAUD Chargé de mission auprès du DRH DGDRD-Drh - TA 174/04 Av Agropolis 34398 Montpellier cedex 5 tel : 04.67.61.75.38 fax : 04.67.61.57.87 port: 06.47.43.55.31 __ 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. -- Pascal Oettli Project Scientist JAMSTEC Yokohama, Japan __ 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.
Re: [R] To transform a vector
Thank you Michel Le 09/12/2013 08:14, Berend Hasselman a écrit : On 09-12-2013, at 08:04, Arnaud Michel michel.arn...@cirad.fr wrote: Dear R Users I have the vector X - c( 6 , 4 ,12 , 3) I would like to build a new vector by to transform it into Y - c(rep(X[1], X[1]), rep(X[2], X[2]), rep(X[3], X[3]), rep(X[4], X[4])) Have you a more elegant answer ? Have a good read of ?rep. Try this: rep(X,times=X) Berend -- Michel ARNAUD Chargé de mission auprès du DRH DGDRD-Drh - TA 174/04 Av Agropolis 34398 Montpellier cedex 5 tel : 04.67.61.75.38 fax : 04.67.61.57.87 port: 06.47.43.55.31 __ 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.