[R] MACRO-LOOP in R

Thierry Onkelinx thierry.onkelinx at inbo.be
Fri Feb 19 09:24:25 CET 2016


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 <amoy_y op yahoo.com>:

> 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.onkelinx op 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 <r-help op r-project.org>:
>
>  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 op 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
> <http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list