[R] Replace Function (How to replace numbers in a data frame with a specific number)

stephen sefick ssefick at gmail.com
Wed Jan 7 16:36:35 CET 2009


taxa <- (structure(list(Date = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("2006/04",
"2006/05", "2006/07", "2006/10", "2006/12", "2007/02", "2007/04",
"2007/06", "2007/08", "2007/10", "2007/12", "2008/01"), class = "factor"),
    RiverMile = c(61L, 119L, 148L, 179L, 185L, 187L, 190L, 196L,
    198L, 202L, 215L, 61L, 119L, 148L, 179L, 185L, 187L, 190L,
    196L, 198L), Site = structure(c(9L, 1L, 2L, 3L, 4L, 10L,
    5L, 11L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 10L, 5L, 11L, 6L
    ), .Label = c("119", "148", "179", "185", "190", "198", "202",
    "215", "61", "BC", "HC", "SC"), class = "factor"), location =
structure(c(2L,
    2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
    1L, 2L, 1L, 2L), .Label = c("creek", "river"), class = "factor"),
    Amphipoda = c(0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1,
    0, 1, 1, 0, 1), Coleoptera = c(1, 1, 4, 1, 0, 1, 1, 2, 1,
    2, 0, 0, 0, 1, 1, 0, 0, 0, 2, 1), Decapoda = c(0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), Diptera = c(6,
    6, 14, 3, 16, 10, 12, 0, 3, 9, 6, 5, 3, 10, 0, 9, 3, 11,
    2, 8), Ephemeroptera = c(5, 6, 4, 1, 3, 1, 3, 1, 0, 2, 0,
    2, 2, 2, 0, 0, 0, 0, 1, 0)), .Names = c("Date", "RiverMile",
"Site", "location", "Amphipoda", "Coleoptera", "Decapoda", "Diptera",
"Ephemeroptera"), row.names = c("61 2006/10", "119 2006/10",
"148 2006/10", "179 2006/10", "185 2006/10", "BC 2006/10", "190 2006/10",
"HC 2006/10", "198 2006/10", "202 2006/10", "215 2006/10", "61 2006/12",
"119 2006/12", "148 2006/12", "179 2006/12", "185 2006/12", "BC 2006/12",
"190 2006/12", "HC 2006/12", "198 2006/12"), class = c("cast_df",
"data.frame")))

replace(taxa, taxa>0, 1)

#Is preforms as I would like except that I only want to do this on
columns 5:19 and I can't just use

replace(taxa, taxa[,5:19]>0, 1)

#any suggestions

Stephen Sefick

-- 
Stephen Sefick

Let's not spend our time and resources thinking about things that are
so little or so large that all they really do for us is puff us up and
make us feel like gods.  We are mammals, and have not exhausted the
annoying little problems of being mammals.

								-K. Mullis




More information about the R-help mailing list