在data.frame(wd)中,我有几列(X1000mb,X950mb,X800mb ...... Xn)。当我在一列上执行cut(as.vector)
函数时(见下文)
wd1<-cut(((as.vector(wd$X850mb)) + 360/(16*2) )%% 360,
seq(0,360,360/16) ,c('N', 'NNE', 'NE', 'ENE',
'E', 'ESE', 'SE', 'SSE', 'S', 'SSW',
'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'))
然而,当我尝试将它应用于整个数据框 - 所有列 - 我收到错误。
wdx<-cut(((as.vector(wd)) + 360/(16*2) )%% 360,
seq(0,360,360/16) ,c('N', 'NNE', 'NE', 'ENE',
'E', 'ESE', 'SE', 'SSE', 'S', 'SSW',
'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'))
错误为Error in cut.default(((as.vector(wd)) + 360/(16 * 2))%%360, seq(0, 360, : 'x' must be numeric
数据框的dput
是
dput(head(wd))
structure(list(X1000mb = c(86L, 130L, 75L, 59L, 56L, 69L), X925mb = c(70L,
45L, 30L, 66L, 54L, 71L), X850mb = c(355L, 349L, 350L, 65L, 36L,
56L), X700mb = c(331L, 342L, 329L, 35L, 1L, 44L), X600mb = c(328L,
328L, 321L, 0L, 247L, 227L), X500mb = c(331L, 324L, 317L, 331L,
251L, 241L), X400mb = c(340L, 328L, 310L, 296L, 261L, 246L),
X300mb = c(336L, 334L, 328L, 295L, 259L, 262L), X250mb = c(334L,
333L, 348L, 300L, 259L, 279L), X200mb = c(336L, 330L, 356L,
331L, 257L, 282L), X150mb = c(333L, 327L, 346L, 342L, 277L,
279L), X100mb = c(317L, 326L, 325L, 318L, 260L, 274L), X70mb = c(323L,
326L, 332L, 306L, 277L, 276L), X50mb = c(350L, 4L, 352L,
328L, 305L, 311L), X30mb = c(5L, 42L, 32L, 15L, 29L, 12L),
X20mb = c(3L, 42L, 48L, 30L, 46L, 45L), X10mb = c(28L, 25L,
4L, 14L, 104L, 76L)), .Names = c("X1000mb", "X925mb", "X850mb",
"X700mb", "X600mb", "X500mb", "X400mb", "X300mb", "X250mb", "X200mb",
"X150mb", "X100mb", "X70mb", "X50mb", "X30mb", "X20mb", "X10mb"
), row.names = c(NA, 6L), class = "data.frame")
我尝试将as.numeric
应用于数据框,但它仍无法正常工作。尝试了一个子集wd[1:17]
它没有用。
我做错了什么?建设性的批评是被接受的。请记住,我还在学习R.谢谢。
答案 0 :(得分:0)
您应该尝试apply
:
apply(wd, 2, function(x) cut(((as.numeric(x)) + 360/(16*2) )%% 360,
+ seq(0,360,360/16) ,c('N', 'NNE', 'NE', 'ENE',
+ 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW',
+ 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW')))
X1000mb X925mb X850mb X700mb X600mb X500mb X400mb X300mb X250mb X200mb X150mb X100mb X70mb X50mb
[1,] "E" "ENE" "N" "NNW" "NNW" "NNW" "NNW" "NNW" "NNW" "NNW" "NNW" "NW" "NW" "N"
[2,] "SE" "NE" "N" "NNW" "NNW" "NW" "NNW" "NNW" "NNW" "NNW" "NNW" "NW" "NW" "N"
[3,] "ENE" "NNE" "N" "NNW" "NW" "NW" "NW" "NNW" "NNW" "N" "NNW" "NW" "NNW" "N"
[4,] "ENE" "ENE" "ENE" "NE" "N" "NNW" "WNW" "WNW" "WNW" "NNW" "NNW" "NW" "NW" "NNW"
[5,] "NE" "NE" "NE" "N" "WSW" "WSW" "W" "W" "W" "WSW" "W" "W" "W" "NW"
[6,] "ENE" "ENE" "NE" "NE" "SW" "WSW" "WSW" "W" "W" "WNW" "W" "W" "W" "NW"
X30mb X20mb X10mb
[1,] "N" "N" "NNE"
[2,] "NE" "NE" "NNE"
[3,] "NNE" "NE" "N"
[4,] "NNE" "NNE" "NNE"
[5,] "NNE" "NE" "ESE"
[6,] "NNE" "NE" "ENE"