Luc
Luc

Reputation: 13

For loop variable as column name

As an example, I have created a small vector containing the column names of a dataframe, I want to loop these variables in order to delete certain values within the column where the variable i equals the column name

Creating the vector:

Ellenberg_value<- c( "VOCHTIND", "ZUURGIND", "STIKSIND", "ZOUT_IND")

For loop:

for (i in Ellenberg_value) {
    Ecobase<- Ecobase[!Ecobase$i== "X",]
    Ecobase<- Ecobase[!Ecobase$i== "?",]
}

I expect the output dataframe wihtout the X's and ? within the columns which are specified in the vector. However, now I get an empty dataframe

Upvotes: 1

Views: 2421

Answers (2)

Lennyy
Lennyy

Reputation: 6132

I don't think a loop is necessary. If I understand you correctly you want to delete the rows in which at least one of the columns "VOCHTIND", "ZUURGIND", "STIKSIND", "ZOUT_IND" equals "X", is that correct?

Ecobase[rowSums(Ecobase[,Ellenberg_value] == "X") == 0,]

If you want to get rid of rows with an "?" as well, you could do:

Ecobase[rowSums(sapply(Ecobase[Ellenberg_value], '%in%', c("X", "?"))) == 0,]

Upvotes: 0

symbolrush
symbolrush

Reputation: 7467

You have to subset with another mechanism (using [[ instead of $), when the column names are stored as character vectors. An example:

df <- data.frame(a = 1 ,b = 3)
cols <- names(df)

The following works as intended:

for (i in cols) {
  print(i)
  print(df[[i]])
}
[1] "a"
[1] 1
[1] "b"
[1] 3

Whereas your code doesn't:

for (i in cols) {
  print(i)
  print(df$i)
}
[1] "a"
NULL
[1] "b"
NULL

In your code you may therefore use Ecobase[!Ecobase[[i]] == "X", ].

Upvotes: 4

Related Questions