plan license examples apps app r shiny shinyapps

license - Convierte cadena en numerado: ¿todos los datos han sido reemplazados cuando solo hay un factor?



shiny server (1)

Después de esta pregunta , tengo un nuevo desafío. Por ejemplo, si tengo este Dataset:

structure(list(particles = structure(c(1L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 5L, 6L, 5L, 3L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("1", "11", "1.1", "2", "2.1", "3.1" ), class = "factor"), date = structure(c(1468833354.929, 1468833365.186, 1468833378.458, 1468833538.213, 1468833538.416, 1468833538.613, 1468833538.81, 1468833538.986, 1468833539.172, 1468833539.358, 1468833539.539, 1468833554.592, 1468833559.059, 1468833562.357, 1468833566.225, 1468833573.486, 1468840019.118, 1468840024.95, 1469029568.849, 1469029584.243, 1469029590.53, 1469029622.391, 1469029623.598, 1469245154.003, 1469245156.533, 1469245156.815, 1469245157.123, 1469245162.358, 1469245165.911, 1469245170.178, 1469245173.788, 1469261432.914, 1469261438.894, 1469261445.18, 1469261447.552, 1469261447.758, 1469261447.967, 1469261448.156, 1469261448.366, 1469261448.583, 1469261448.837, 1469261449.08, 1469261454.697, 1469261458.774, 1469261458.988, 1469261459.312, 1469261466.822, 1469261470.314, 1469261472.048, 1469261472.256, 1469261472.461, 1469261472.84, 1469261473.013, 1469261473.207, 1469261473.379, 1469261473.566, 1469261473.761, 1469261473.951 ), class = c("POSIXct", "POSIXt"), tzone = "Asia/Kuala_Lumpur")), .Names = c("particles", "date"), row.names = c(NA, -58L), class = "data.frame")

Convierta todas las columnas a numnberic:

vec <- sapply(data, is.factor) data[, vec] <- lapply(data[, vec], function(x) as.numeric(as.character(x)))

Advertencia:

Warning message: In `[<-.data.frame`(`*tmp*`, , vec, value = list(1, 1.1, 2.1, 2.1, : provided 58 variables to replace 1 variables

Los datos ahora se han convertido en:

structure(list(particles = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), date = structure(c(1468833354.929, 1468833365.186, 1468833378.458, 1468833538.213, 1468833538.416, 1468833538.613, 1468833538.81, 1468833538.986, 1468833539.172, 1468833539.358, 1468833539.539, 1468833554.592, 1468833559.059, 1468833562.357, 1468833566.225, 1468833573.486, 1468840019.118, 1468840024.95, 1469029568.849, 1469029584.243, 1469029590.53, 1469029622.391, 1469029623.598, 1469245154.003, 1469245156.533, 1469245156.815, 1469245157.123, 1469245162.358, 1469245165.911, 1469245170.178, 1469245173.788, 1469261432.914, 1469261438.894, 1469261445.18, 1469261447.552, 1469261447.758, 1469261447.967, 1469261448.156, 1469261448.366, 1469261448.583, 1469261448.837, 1469261449.08, 1469261454.697, 1469261458.774, 1469261458.988, 1469261459.312, 1469261466.822, 1469261470.314, 1469261472.048, 1469261472.256, 1469261472.461, 1469261472.84, 1469261473.013, 1469261473.207, 1469261473.379, 1469261473.566, 1469261473.761, 1469261473.951 ), class = c("POSIXct", "POSIXt"), tzone = "Asia/Kuala_Lumpur")), .Names = c("particles", "date"), row.names = c(NA, -58L), class = "data.frame")

Como puede ver, todos los datos en las particles columna particles han convertido en:

1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1

Cuál está mal. ¿Cómo puedo arreglar esto?

¿Por qué reemplaza todos los datos a 1 cuando se trata de columnas de un solo factor para convertir? Está bien cuando tengo dos o más columnas de factores para convertir.


Utilizar esta

data[vec] <- lapply(data[vec], function(x) as.numeric(as.character(x)))