Hi I want to do something like a <- c(10,20,15,43,76,41,25,46) tab <- data.frame(a) name <- "a" for (v in unique(tab[[name]])) { r <- subset(tab, name==v) # this does not work ... } i.e. a "string" on the left side of the select expression (subset). How could I solve this? thx Christof
Hi the output should look like r <- subset(tab, a==v) but now I have something like r <- subset(tab, "a"==v) and r <- subset(tab, [["a"]]==v) does not work :( greetings Christof Am 05-01-2012 16:51, schrieb Christof Klu?:> Hi > > I want to do something like > > a <- c(10,20,15,43,76,41,25,46) > tab <- data.frame(a) > > name <- "a" > > for (v in unique(tab[[name]])) { > r <- subset(tab, name==v) # this does not work > ... > } > > i.e. a "string" on the left side of the select expression (subset). How > could I solve this? > > thx > Christof >
Le jeudi 05 janvier 2012 ? 16:51 +0100, Christof Klu? a ?crit :> Hi > > I want to do something like > > a <- c(10,20,15,43,76,41,25,46) > tab <- data.frame(a) > > name <- "a" > > for (v in unique(tab[[name]])) { > r <- subset(tab, name==v) # this does not work > ... > } > > i.e. a "string" on the left side of the select expression (subset). How > could I solve this? > > the output should look like r <- subset(tab, a==v) > but now I have something like r <- subset(tab, "a"==v) > and r <- subset(tab, [["a"]]==v) > does not work :(Not sure what you want to do. The subset() commands you're using will select the rows from tab that match the given expression, which in your first example is "name==v". The problem is, none of the values of tab is equal to "a" (the value of 'name'), so it returns nothing. You can check it by just typing: name==v So, please tell us what you want to achieve, rather than how you're doing it (wrong). ;-) Cheers
Hi thank you very much for your useful answers! In this case I solved it with Sarah's suggestion tab[tab[[name]] == v,] that works fine Greetings Christof Am 05-01-2012 16:51, schrieb Christof Klu?:> Hi > > I want to do something like > > a <- c(10,20,15,43,76,41,25,46) > tab <- data.frame(a) > > name <- "a" > > for (v in unique(tab[[name]])) { > r <- subset(tab, name==v) # this does not work > ... > } > > i.e. a "string" on the left side of the select expression (subset). How > could I solve this? > > thx > Christof >