Re: [R] MACRO-LOOP in R

2016-02-19 Thread Thierry Onkelinx
Please keep the mailinglist in cc.

You should be able to solve this after reading the helpfiles of
?dplyr::mutate and ?tidyr::spread

Best regards,


ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2016-02-18 18:44 GMT+01:00 Amoy Yang :

> So simply. What happens if I have multiple columns (b, d and e) to be
> transposed and renamed as you did for b? Thanks again for helps!
>
> x<-data.frame(
>  a=c(1,2,3),
>  b=c("1","2","3"),
>  d=c(5,6,7),
>  e=c("n1","n2","n3"));
> x; str(x)
>
>
> On Thursday, February 18, 2016 10:37 AM, Thierry Onkelinx <
> thierry.onkel...@inbo.be> wrote:
>
>
> R has no concept of macro language like SAS because it doesn't need it.
> Here is a solution for your problem.
>
> library(dplyr)
> library(tidyr)
> x %>%
>   mutate(b = paste0("b_", b)) %>%
>   spread(key = b, value = a)
>
> Best regards,
>
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
> Forest
> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
> Kliniekstraat 25
> 1070 Anderlecht
> Belgium
>
> To call in the statistician after the experiment is done may be no more
> than asking him to perform a post-mortem examination: he may be able to say
> what the experiment died of. ~ Sir Ronald Aylmer Fisher
> The plural of anecdote is not data. ~ Roger Brinner
> The combination of some data and an aching desire for an answer does not
> ensure that a reasonable answer can be extracted from a given body of data.
> ~ John Tukey
>
> 2016-02-18 17:17 GMT+01:00 Amoy Yang via R-help :
>
>  I am doing the data transpose with rename as shown below (step1 ~ step4)
> 1. Is any way in R similar to PROC TRANSPOSE used in SAS?2. How to use
> MACRO-LOOP to simplify the following procedure?
> THANK YOU FOR HELPS!
> # create data for test
> x<-data.frame(
>  a=c(1,2,3),
>  b=c("1","2","3"));
> x; str(x)# step1: parse out to 3 tabs
> x1<-x[x$a == 1,]; x1
> x2<-x[x$a == 2,]; x2
> x3<-x[x$a == 3,]; x3# step2: remove column a in each tab
> x1$a<-NULL; x1
> x2$a<-NULL; x2
> x3$a<-NULL; x3# step3: rename column b to b1, b2 and b3 by y1, y2 and y3
> names(x1)[names(x1)=="b"]<-"b_1"; x1
> names(x2)[names(x2)=="b"]<-"b_2"; x2
> names(x3)[names(x3)=="b"]<-"b_3"; x3# setp4: set x1, x3 and x3 together
> x123=cbind(x1,x2,x3); x123
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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] MACRO-LOOP in R

2016-02-18 Thread Jeff Newmiller
What a mess. Transposing factors is highly unlikely to lead to sensible 
results. Did you look at str( x123 ) as your example created it? 

x <- data.frame( a=c(1,2,3), b=c("1","2","3"), stringsAsFactors=FALSE )

x123new <- setNames( as.data.frame( t( x[ , "b" ] ) ), paste( "b", 1:3, sep="_" 
) )

R is not SAS. Please read the Introduction to R document (again). Pay attention 
to the discussions of indexing, character mode and factors. 

-- 
Sent from my phone. Please excuse my brevity.

On February 18, 2016 8:17:47 AM PST, Amoy Yang via R-help 
 wrote:
>I am doing the data transpose with rename as shown below (step1 ~
>step4)
>1. Is any way in R similar to PROC TRANSPOSE used in SAS?2. How to use
>MACRO-LOOP to simplify the following procedure?
>THANK YOU FOR HELPS!
># create data for test
>x<-data.frame(
> a=c(1,2,3),
> b=c("1","2","3")); 
>x; str(x)# step1: parse out to 3 tabs
>x1<-x[x$a == 1,]; x1
>x2<-x[x$a == 2,]; x2
>x3<-x[x$a == 3,]; x3# step2: remove column a in each tab
>x1$a<-NULL; x1
>x2$a<-NULL; x2
>x3$a<-NULL; x3# step3: rename column b to b1, b2 and b3 by y1, y2 and
>y3
>names(x1)[names(x1)=="b"]<-"b_1"; x1
>names(x2)[names(x2)=="b"]<-"b_2"; x2
>names(x3)[names(x3)=="b"]<-"b_3"; x3# setp4: set x1, x3 and x3 together
>x123=cbind(x1,x2,x3); x123
>   [[alternative HTML version deleted]]
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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 -- To UNSUBSCRIBE and more, see
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] MACRO-LOOP in R

2016-02-18 Thread Amoy Yang via R-help
 I am doing the data transpose with rename as shown below (step1 ~ step4)
1. Is any way in R similar to PROC TRANSPOSE used in SAS?2. How to use 
MACRO-LOOP to simplify the following procedure?
THANK YOU FOR HELPS!
# create data for test
x<-data.frame(
 a=c(1,2,3),
 b=c("1","2","3")); 
x; str(x)# step1: parse out to 3 tabs
x1<-x[x$a == 1,]; x1
x2<-x[x$a == 2,]; x2
x3<-x[x$a == 3,]; x3# step2: remove column a in each tab
x1$a<-NULL; x1
x2$a<-NULL; x2
x3$a<-NULL; x3# step3: rename column b to b1, b2 and b3 by y1, y2 and y3
names(x1)[names(x1)=="b"]<-"b_1"; x1
names(x2)[names(x2)=="b"]<-"b_2"; x2
names(x3)[names(x3)=="b"]<-"b_3"; x3# setp4: set x1, x3 and x3 together
x123=cbind(x1,x2,x3); x123
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.