R iterating over dataframe columns

I want to apply statistics to iteration column DataFrame:

columns number 1: "A" represents the tags that I want to distinguish:

for (i in names(dataframe)) { i <- as.name(i) group1 <- i[A=="locationX"] group2 <- i[A!="locationX"] p <- wilcox.test(group1,group2,na.action(na.omit))$p.value } 

however, as.name() should try to remove the inverted commas from the column names(dataframe) generated by names(dataframe) .

Unfortunately this gives me an error:

Error in I [A == "locationX"]: an object of type "character" is not a subset of

I think as.name() is the wrong way to do this.

Any clues?

+6
source share
1 answer

The only way that makes sense is for “A” to be a vector with multiple instances of “locationX” and a multiple instance of the inverse and for length “A” to be the same as the number of rows in the “dataframe”. If so, then something like this may result:

 p <- list() for (i in names(dataframe)) { # using as.names not needed and possibly harmful group1 <- dataframe[[i]][A == "locationX"] group2 <- dataframe[[i]][A != "locationX"] p[i] <- wilcox.test(group1,group2,na.action(na.omit))$p.value } 

Please note that even if you did not get an error with a code that you would still rewrite "p" every time through the loop.

+8
source

Source: https://habr.com/ru/post/906244/


All Articles