[R] recode based on filter

Henrique Dallazuanna wwwhsd at gmail.com
Wed Dec 19 13:12:16 CET 2007


Perhaps you can try subset the data:

sapply(levels(DATA$know1), function(x)subset(DATA, (know1==x &
know2==x)), simplify=F)

On 19/12/2007, Donatas G. <dgvirtual at akl.lt> wrote:
> Hi, I have a data frame DATA, which (simplified of course) looks like this:
>
> know1 = c("Y","N","N","Y","N","N","Y","Y","N")
> par1=c(1,4,5,3,3,2,3,3,5)
> know2 = c("Y","Y","N","Y","N","N","N","Y","Y")
> par2=c(3,4,4,3,5,2,4,3,2)
> DATA=data.frame(know1,par1,know2,par2)
>
> it represents answers in a questionnaire, where respondents evaluate two
> things (par1 and par2) but they also indicate, whether they have detailed
> knowledge about those two things (know1 and know2).
>
> I need to test correlations between parameters par1 and par2, but need to do
> that separately for cases where the respondents know about both things and
> where they do not know about both things. If, testing correlations of those
> who do know, a respondent knows only about item1 and not item2 (if the
> responces do not match), the values of par1 and par2 should be changed to
> NA's. The same goes if the respondent says he does not know only about one
> thing.
>
> SO: Before doing analysis, I need to transform the DATA dataframe in two ways:
>
> Testing correlations of those evaluating with knowledge:
>
> > DATA
>  know1 par1 know2 par2
> 1     Y    1     Y    3
> 2     N    NA    Y    NA
> 3     N    NA    N    NA
> 4     Y    3     Y    3
> 5     N    NA    N    NA
> 6     N    NA    N    NA
> 7     Y    NA    N    NA
> 8     Y    3     Y    3
> 9     N    NA    Y    NA
>
> And the case of those evaluating without knowledge:
>
> > DATA
>  know1 par1 know2 par2
> 1     Y    NA    Y    NA
> 2     N    NA    Y    NA
> 3     N    5     N    4
> 4     Y    NA    Y    NA
> 5     N    3     N    5
> 6     N    2     N    2
> 7     Y    NA    N    NA
> 8     Y    NA    Y    NA
> 9     N    NA    Y    NA
>
> Yes, yes, this example is stupid, but...
>
> I know how to filter records, I know how to recode, but I need some
> combination of the two here and I am lost.. Any ideas?
>
> --
> Donatas Glodenis
> http://dg.lapas.info
>
> ______________________________________________
> 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.
>


-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list