[R] assign()ing within apply
Thompson, David (MNR)
David.John.Thompson at ontario.ca
Tue Dec 9 22:03:21 CET 2008
Hello,
I'm trying to convert a character column in several dataframes to lower
case.
###
#
# Sample data and 'spp' column summaries:
# dput(ban.ovs.1993[sample(row.names(ban.ovs.1993), 20), 1:4])
ban.ovs.93 <- structure(list(oplt = c(43L, 43L, 38L, 26L, 35L, 8L, 39L,
1L,
34L, 50L, 10L, 29L, 31L, 24L, 18L, 12L, 27L, 49L, 28L, 51L),
rplt = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_), tree = c(427L, 410L, 639L, 494L, 649L, 166L,
735L, 163L, 120L, 755L, 612L, 174L, 129L, 331L, 269L, 152L,
552L, 227L, 243L, 96L), spp = c("MH", "MST", "MH", "HE",
"BE", "MH", "MH", "MH", "MH", "Or", "IW", "Or", "MH", "MH",
"BY", "MH", "MH", "BE", "MH", "MR")), .Names = c("oplt",
"rplt", "tree", "spp"), row.names = c(4587L, 4570L, 3947L, 2761L,
3653L, 652L, 4136L, 64L, 3567L, 5318L, 838L, 3091L, 3366L, 2423L,
1775L, 1061L, 2893L, 5161L, 2967L, 5395L), class = "data.frame")
# dput(pem.ovs.1994[sample(row.names(pem.ovs.1994), 20), 1:4])
pem.ovs.94 <- structure(list(oplt = c(8L, 17L, 36L, 9L, 31L, 11L, 35L,
51L,
51L, 49L, 40L, 1L, 9L, 17L, 4L, 42L, 6L, 3L, 39L, 25L), tree = c(531L,
557L, 546L, 261L, 592L, 134L, 695L, 933L, 945L, 114L, 34L, 54L,
549L, 574L, 193L, 96L, 70L, 4L, 546L, 789L), spp = c("MH", "MH",
"MH", "BF", "BF", "MH", "IW", "OR", "OR", "BF", "MH", "IW", "OR",
"MH", "SM", "BE", "BE", "BE", "OR", "OR"), oaz = c(38L, 205L,
140L, 277L, 329L, 209L, 222L, 24L, 67L, 187L, 156L, 181L, 174L,
248L, 42L, 279L, 273L, 357L, 160L, 183L)), .Names = c("oplt",
"tree", "spp", "oaz"), row.names = c(1204L, 2943L, 5790L, 1616L,
5063L, 2013L, 5691L, 8188L, 8200L, 7822L, 6302L, 54L, 1698L,
2960L, 421L, 6690L, 775L, 245L, 6205L, 4121L), class = "data.frame")
# count per spp
invisible(lapply(ls(pat='ovs'), function(y) { cat(y, "\n") ;
print(summary.factor(get(y)[,'spp'])) ; cat("\n") } ) )
ban.ovs.93
BE BY HE IW MH MR MST Or
2 1 1 1 11 1 1 2
pem.ovs.94
BE BF IW MH OR SM
3 3 2 6 5 1
#
###
I have tried variants on the following and cannot remember how to have
the result assign()ed back to the original dataframes.
lapply(ls(pat='ovs'), function(y) { assign(paste(y, "[,'spp']", sep=""),
tolower(get(y)[,'spp'])) } )
I *do* get the expected results:
[[1]]
[1] "mh" "mst" "mh" "he" "be" "mh" "mh" "mh" "mh" "or" "iw"
"or" "mh" "mh" "by" "mh" "mh" "be" "mh" "mr"
[[2]]
[1] "mh" "mh" "mh" "bf" "bf" "mh" "iw" "or" "or" "bf" "mh" "iw" "or"
"mh" "sm" "be" "be" "be" "or" "or"
, I just can't remember how to get them back into the original dataframe
objects. Suggestions?
Thanx, DaveT.
*************************************
Silviculture Data Analyst
Ontario Forest Research Institute
Ontario Ministry of Natural Resources
david.john.thompson at ontario.ca
http://ontario.ca/ofri
More information about the R-help
mailing list