[R] Odp: Help with factors

Petr PIKAL petr.pikal at precheza.cz
Wed May 14 13:23:34 CEST 2008


Hi

I am not sure what you want to do as you speak about using different 
function for different part of your data.


> head(data)
  city year sex       V
1    1 1975   F 25.3044
2    1 1975   M 16.5711
3    1 1976   M 16.6072
4    1 1976   F 24.2841
5    1 1977   M 14.8838
6    1 1977   F 24.8124

If it was only correlation coefficients you could use lapply split 
construction like

lapply(split(data[,c(4,2)], interaction(data$city, data$sex)), function(x) 
summary(lm(x))$r.sq)

$`1.M`
[1] 0.923669

$`2.M`
[1] 0.5151131

$`1.F`
[1] 0.823817

$`2.F`
[1] 0.7483883

or

> lapply(split(data[,c(4,2)], interaction(data$city, data$sex)), cor)
$`1.M`
             V      year
V     1.000000 -0.961077
year -0.961077  1.000000

$`2.M`
              V       year
V     1.0000000 -0.7177138
year -0.7177138  1.0000000

Regards

Petr Pikal
petr.pikal at precheza.cz
724008364, 581252140, 581252257


r-help-bounces at r-project.org napsal dne 14.05.2008 11:46:02:

> Hallo All,
> 
> I have difficulties understanding how factors work in R. Suppose a have 
> data in the panel form below. I would to compute a correlation 
coefficient 
> (actually apply a different function of two time series) in the V 
variable 
> between members of the two sexes in each city over time. How can this be 

> done?
> 
> Thank you in advance,
> Serguei
> 
> city, year, sex, V
> 1, 1975, 1, 25.3044
> 1, 1975, 0, 16.5711
> 1, 1976, 0, 16.6072
> 1, 1976, 1, 24.2841
> 1, 1977, 0, 14.8838
> 1, 1977, 1, 24.8124
> 1, 1978, 1, 23.0570
> 1, 1978, 0, 14.5627
> 1, 1979, 1, 21.2071
> 1, 1979, 0, 13.5277
> 2, 1975, 1, 62.4457
> 2, 1975, 0, 26.9745
> 2, 1976, 1, 67.3025
> 2, 1976, 0, 31.4600
> 2, 1977, 1, 53.0577
> 2, 1977, 0, 25.1941
> 2, 1978, 0, 23.3694
> 2, 1978, 1, 40.1452
> 2, 1979, 1, 44.5686
> 2, 1979, 0, 23.4042
> 
> ______________________________________________
> 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