[R] Macro or Loop info/help needed

Petr PIKAL petr.pikal at precheza.cz
Thu Mar 22 10:56:26 CET 2012


Hi

It is better instead of vague description of your file to use dput(t4) or 
at least str(t4) for showing internals.

If I understand correctly your data frame will have columns eventXyear, 
eventZyear... and qXayr. qXayr have values 73, 74, ... and you want to 
change values in eventXyear to 1 according to value from qXayr?

Seems to me rather strange operation. What do you want to do after this 
huge expansion? If you have 20 columns with 16 value you will get 20x16 = 
320 new columns coded 0/1.

As you did not provide any data for playing with I made my own.

t4<-data.frame(matrix(sample(73:88, 60, replace=T),10,6))
> t4
   X1 X2 X3 X4 X5 X6
1  75 87 84 76 82 77
2  86 83 81 79 78 82
3  82 86 86 84 84 84
4  73 87 80 85 74 83
5  85 84 73 78 75 87
6  84 73 83 87 77 79
7  78 85 73 81 83 85
8  78 85 75 74 75 83
9  87 76 83 86 74 82
10 87 81 73 87 78 85

# first I changed it to list
lll<-as.list(t4)

# than change 73:88 codes to values 1/0
test<-as.data.frame(lapply(lll, function(x) 73:88 %in% x))*1
row.names(test)<-73:88
test
   X1 X2 X3 X4 X5 X6
73  1  1  1  0  0  0
74  0  0  0  1  1  0
75  1  0  1  0  1  0
76  0  1  0  1  0  0
77  0  0  0  0  1  1
78  1  0  0  1  1  0
79  0  0  0  1  0  1
80  0  0  1  0  0  0
81  0  1  1  1  0  0
82  1  0  0  0  1  1
83  0  1  1  0  1  1
84  1  1  1  1  1  1
85  1  1  0  1  0  1
86  1  1  1  1  0  0
87  1  1  0  1  0  1
88  0  0  0  0  0  0

# expand to 96 rows, transpose and give some column names
library(reshape)
t4m<-melt(t(test))
columns<-paste("event", t4m[,1], t4m[,2]+1900, sep="")
tfinal<-as.data.frame(t(t4m[,3]))
colnames(tfinal)<-columns

> tfinal
  eventA1973 eventB1973 eventC1973 eventD1973 eventE1973 eventF1973 
eventA1974
1          1          1          1          0          0          0   0
  eventB1974 eventC1974 eventD1974 eventE1974 eventF1974 eventA1975 
eventB1975
1....

I know this is not a result you expect but why you want to expand this row 
to all rows of your first file?

Regards
Petr

> 
> 
> 
>    I'm very new to R having recently made the transition from SPSS and 
SAS.
>    In  a  dataset named t4, I have about 20 variables that are named in 
a
>    somewhat chronological order - (e.g., q100ayr, q101ayr, q102ayr, 
q103ayr,
>    etc.)
>    Each variable contains a 2 digit year designation (e.g., 73, 74,75, 
76,
>    etc), which corresponds to the year something occurred (e.g., 
73=1973).
>    Each of the 20 variables is similarly designated, but corresponds to 
a
>    different type of event/occurrence.
>    I need to reorder the data in this fashion:
>    # first event
>    t4 $ eventX1973 <- 0
>    t4$ eventX1973[t4 $ q100ayr== 73 ] <- 1
>    t4 $ eventX1974 <- 0
>    t4$ eventX1974[t4 $ q100ayr== 74 ] <- 1
>    t4 $ eventX1975 <- 0
>    t4$ eventX1975[t4 $ q100ayr== 75 ] <- 1
>    etc. for years between 73 and 88
>    # second event
>    t4 $ eventZ1973 <- 0
>    t4$ eventZ1973[t4 $ q101ayr== 73 ] <- 1
>    t4 $ eventZ1974 <- 0
>    t4$ eventZ1974[t4 $ q101ayr== 74 ] <- 1
>    t4 $ eventZ1975 <- 0
>    t4$ eventZ1975[t4 $ q101ayr== 75 ] <- 1
>    etc.
>    The code above will work, but would be many lines long. I'm hoping 
someone
>    can give me a quick introduction to what would work best in R. I 
assume some
>    type of macro or loop.
>    Thanks in advance.
>    Jeff
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list