我有数据帧df.1和df.2(从实际数据帧中提取)
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")
尝试将pmin或pmax函数应用于这些数据帧会导致错误:
pmax (df.1, df.2)
Error in `[.data.frame`(each, nas[, 1L]) : undefined columns selected
我完全不知道它为什么会发生,pmax和pmin在我的实际数据帧的其他部分工作得很好,它们看起来非常类似于这些数据。我非常感谢你的帮助。
以下是会话信息:
(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
答案 0 :(得分:3)
在运行matrix
或pmax
之前,尝试将这两个数据框转换为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
来源:?pmax
“pmax和pmin将一个或多个向量(或矩阵)作为参数......”
答案 1 :(得分:1)
尚不确定(尚未)如何解决此问题,但似乎是围绕在比较的第一个数据集中出现的NAs。您可以通过一次更改一个NA并尝试重新运行代码来确认这一点。
解决方法是创建一个首先考虑的虚拟data.frame
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)