R で空のセルをゼロ (0) に置き換える必要があります。次のようなデータ フレームがあります。
dput(df)
structure(list(CHANNEL = structure(c(1L, 1L, 1L), .Label = "Native BlackBerry App", class = "factor"),
DATE = structure(c(1L, 1L, 1L), .Label = "01/01/2011", class = "factor"),
HOUR = structure(c(3L, 1L, 2L), .Label = c("1:00am-2:00am",
"2:00am-3:00am", "Midnight-1:00am"), class = "factor"), UNIQUE_USERS = structure(c(1L,
1L, 1L), .Label = "", class = "factor"), LOGON_VOLUME = structure(c(1L,
1L, 1L), .Label = "", class = "factor")), .Names = c("CHANNEL",
"DATE", "HOUR", "UNIQUE_USERS", "LOGON_VOLUME"), row.names = c(NA,
-3L), class = "data.frame")
私はこの機能を持っています:
sapply(df, function (x)
as.numeric(gsub("(^ +)|( +$)", "0", x)))
これらのエラーが発生しますが、機能していません。
[ reached getOption("max.print") -- omitted 422793 rows ]
Warning messages:
1: In FUN(X[[4L]], ...) : NAs introduced by coercion
2: In FUN(X[[4L]], ...) : NAs introduced by coercion
3: In FUN(X[[4L]], ...) : NAs introduced by coercion
4: In FUN(X[[4L]], ...) : NAs introduced by coercion
更新:この関数をdfに適用すると:
sapply(df, function (x) gsub("(^ +)|( +$)", "0", x) )
私はこれを得る:
CHANNEL DATE HOUR UNIQUE_USERS LOGON_VOLUME
[1,] "Native BlackBerry App" "01/01/2011" "Midnight-1:00am" "" ""
[2,] "Native BlackBerry App" "01/01/2011" "1:00am-2:00am" "" ""
[3,] "Native BlackBerry App" "01/01/2011" "2:00am-3:00am" "" ""