简体   繁体   中英

Error in pmin and pmax “Error in `[.data.frame`(each, nas[, 1L]) : undefined columns selected”

I have dataframes df.1 and df.2 (extracts from real data frames)

df.1<-structure(list(a = c(47.22, 88.89, 71.88, NA, 75, 100, 72.22, 
94.44, 52.78, 86.11, 66.67), b = c(46.88, 80.56, 80.56, NA, 31.25, 
100, 52.78, 100, 31.25, 91.67, 58.33)), .Names = c("a", "b"), row.names = 2330:2340, class = "data.frame")

df.2<-structure(list(a = c(75, 47.22, 86.11, 93.75, 86.11, 77.78, 91.67, 
100, 38.89, 97.22, 100), b = c(71.88, 72.22, 83.33, NA, 50, 69.44, 
100, 100, 53.13, 91.67, 88.89)), .Names = c("a", "b"), row.names = 2330:2340, class =   "data.frame")

Trying to apply pmin or pmax function to these data frames results in error:

 pmax (df.1, df.2)
 Error in `[.data.frame`(each, nas[, 1L]) : undefined columns selected

I have absolutely no idea why it is happening, pmax and pmin work great on other parts of my real data frames and they look quite similar to these pieces of data. I would very much appreciate your help.

Here is the session info:

(sessionInfo())
 R version 2.14.2 (2012-02-29)
 Platform: i386-pc-mingw32/i386 (32-bit)

 locale:
 [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252        LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] stringr_0.6.2    plyr_1.8         Revobase_6.1.0   RevoMods_6.1.0     RevoScaleR_3.1-0 lattice_0.20-0   rpart_3.1-51    

loaded via a namespace (and not attached):
[1] codetools_0.2-8 foreach_1.4.0   grid_2.14.2     iterators_1.0.6 tools_2.14.2 

Try converting both data frames into a matrix before running pmax or pmin :

pmax(as.matrix(df.1), as.matrix(df.2))

#         a      b
# 2330  75.00  71.88
# 2331  88.89  80.56
# 2332  86.11  83.33
# 2333     NA     NA
# 2334  86.11  50.00
# 2335 100.00 100.00
# 2336  91.67 100.00
# 2337 100.00 100.00
# 2338  52.78  53.13
# 2339  97.22  91.67
# 2340 100.00  88.89

Source: ?pmax "pmax and pmin take one or more vectors (or matrices) as arguments..."

Not sure (yet) how to resolve the issue, but it appears to be surrounding NAs appearing within the first dataset being compared. You can confirm this by changing the NAs one at a time and trying to rerun the code.

A workaround is to create a dummy data.frame that gets considered first

df.0<-structure(list(a =rep(0,11),b=rep(0,11)), .Names = c("a", "b"), row.names =  2330:2340, class = "data.frame")
df.1<-structure(list(a = c(47.22, 88.89, 71.88, NA, 75, 100, 72.22, 
                       94.44, 52.78, 86.11, 66.67), b = c(46.88, 80.56, 80.56, NA, 31.25, 
                       100, 52.78, 100, 31.25, 91.67, 58.33)), .Names = c("a", "b"), row.names = 2330:2340, class = "data.frame")

df.2<-structure(list(a = c(75, 47.22, 86.11, 93.75, 86.11, 77.78, 91.67, 
                           100, 38.89, 97.22, 100), b = c(71.88, 72.22, 83.33, NA, 50, 69.44, 
                            100, 100, 53.13, 91.67, 88.89)), .Names = c("a", "b"), row.names = 2330:2340, class =   "data.frame")

pmax(df.0,df.1, df.2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM