Re: [R] shrink list by mathed entries
On 14.11.2009, at 03:58, David Winsemius wrote: On Nov 13, 2009, at 11:19 AM, soeren.vo...@eawag.ch wrote: a <- c("Mama", "Papa", "Papa; Mama", "", "Sammy; Mama; Papa") a <- strsplit(a, "; ") mama <- rep(F, length(a)) mama[sapply(a, function(x) { sum(x=="Mama") }, simplify=T) > 0] <- T [...] ... produces the variables "mama" and "papa" correctly. But how do I remove all "Mama" list entries [...] Maybe you should explain what you were trying to do? Perhaps: > a[!mama] [...] I would sidestep that confusing sequence of logical assignments and just do this: > a[ -grep("Mama", a) ] [...] Explanation of what I want to do: This code is PHP, maybe rather crude but it works the way I want it and explains my goal: #!/usr/bin/php $strings = array("Mama", "Papa", "Papa; Mama", "", "Sammy; Mama; Papa", "Josh", "Mama"); $vars = array("Mama", "Papa", "Sammy"); $i=0; foreach($strings as $line){ $line = explode("; ", $line); $matches = array_intersect($line, $vars); $diffs = array_diff($line, $vars); foreach($matches as $match){ eval("\$$match"."["."$i"."] = 1;"); // no easier way } foreach($diffs as $diff){ $others[$i] = $diff; } $i++; } print_r($Mama); // array with elements 0, 2, 4, and 6 set to "1" print_r($Papa); // array with elements 1, 2, and 4, set to "1" print_r($Sammy); // array with element 4 set to "1" print_r($others); // array with elements 3 set to "", and 5 set to "Josh" ?> Sören __ R-help@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.
Re: [R] shrink list by mathed entries
On Nov 13, 2009, at 11:19 AM, soeren.vo...@eawag.ch wrote: Hello a <- c("Mama", "Papa", "Papa; Mama", "", "Sammy; Mama; Papa") a <- strsplit(a, "; ") mama <- rep(F, length(a)) mama[sapply(a, function(x) { sum(x=="Mama") }, simplify=T) > 0] <- T papa <- rep(F, length(a)) papa[sapply(a, function(x) { sum(x=="Papa") }, simplify=T) > 0] <- T # ... more variables ... produces the variables "mama" and "papa" correctly. But how do I remove all "Mama" list entries in "a" in the same run, that is, shrink the list by what was already matched? Maybe you should explain what you were trying to do? Perhaps: > a[!mama] [[1]] [1] "Papa" [[2]] character(0) I would sidestep that confusing sequence of logical assignments and just do this: > a[ -grep("Mama", a) ] [[1]] [1] "Papa" [[2]] character(0) -- David Winsemius, MD Heritage Laboratories West Hartford, CT __ R-help@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.
[R] shrink list by mathed entries
Hello a <- c("Mama", "Papa", "Papa; Mama", "", "Sammy; Mama; Papa") a <- strsplit(a, "; ") mama <- rep(F, length(a)) mama[sapply(a, function(x) { sum(x=="Mama") }, simplify=T) > 0] <- T papa <- rep(F, length(a)) papa[sapply(a, function(x) { sum(x=="Papa") }, simplify=T) > 0] <- T # ... more variables ... produces the variables "mama" and "papa" correctly. But how do I remove all "Mama" list entries in "a" in the same run, that is, shrink the list by what was already matched? Thank you for your help! Sören Vogel -- Sören Vogel, Dipl.-Psych. (Univ.), PhD-Student, Eawag, Dept. SIAM http://www.eawag.ch, http://sozmod.eawag.ch __ R-help@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.