[R] Attempting to recode elements contained in a list

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Aug 25 08:28:11 CEST 2005


On Wed, 24 Aug 2005, Greg Blevins wrote:

> Hello R-Masters,
>
> I have a list 's' with three elements, as shown below.  I want to recode 
> a.a, a.a2, and a.a3 to NA if the value in a.a is less than 3.

I think s is a data frame, and you are looking at its printout.  There is 
almost certainly no element a.a.  Rather, the data frame has a column a, 
which is probably a matrix with three columns, a, a2 and a3. Use str(s) to 
be sure.  Here is an attempt to create a similar object

> s <- data.frame(scan=letters[1:3], time=LETTERS[1:3])
> s$a <- array(runif(9)*6, dim=c(3,3), dimnames=list(NULL, c("a", "a2", 
"a3")))
> s
   scan time      a.a     a.a2     a.a3
1    a    A 5.435430 1.283933 1.546180
2    b    B 5.169964 2.519836 1.568225
3    c    C 3.844045 2.318089 0.772869

If that is the case, you need

s$a[s$a[, "a"] < 3, ] <- NA


> I reivewed my Modern Applied Statistic Book, the online help and did 
> some searching of R-help on the internet. I explored unlist and 
> as.list.data.frame in an attempt to isolate the third element of the 
> list s, but this was not helpful. Any help would be appreciated.
>
>> dim(s)
> [1] 342   3
>> names(s)
> [1] "scan" "time" "a"
>
>> s
>                  scan               time a.a a.a2 a.a3
> 202       Plastic lids       Plastic lids NaN  NaN  NaN
> 195       Plastic lids      Cookware lids  44   31   58
> 205       Plastic lids  Seasoning packets   9    4   20
> 191       Plastic lids      Baking sheets  15    7   27
> 209       Plastic lids           Utensils   9    4   20
> 194       Plastic lids        Cell phones   0    0    7
> 193       Plastic lids       Canned goods   0    0    7
> 197       Plastic lids    Hand/dish towel   3    0   12
>
> Thank you.
>
> Greg Blevins
> The Market Solutions Group
>
> Windows xp, 2.1.1
>
> Gregory L. Blevins
> Vice President, Partner
> The Market Solutions Group, Inc.
> gblevins at marketsolutionsgroup.com
> Office phone: 612 392-3163
> Cell phone: 612 251-0232
> "What is at the beginning but a small error, swells to huge proportions at the close." Aristotle
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list