2

Following this question I have a new challenge. For instance, if I have this 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")

Convert all columns to numnberic:

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

Warning:

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

The data now has become:

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")

As you can see that all data in column particles has become:

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

Which is wrong. How can I fix this?

Why does it replace all the data to 1 when this is only one factor columns to convert? It is fine when I have two or more factor columns to convert.

Community
  • 1
  • 1
Run
  • 54,938
  • 169
  • 450
  • 748
  • 7
    Just add `drop = FALSE`, e.g. `lapply(data[, vec, drop = FALSE]...` to prevent R from coercing the subset to vector and treat it as a data.frame, instead. – Roman Luštrik Jul 23 '16 at 11:20

1 Answers1

1

Use this

data[vec] <- lapply(data[vec], function(x) as.numeric(as.character(x)))
user2100721
  • 3,557
  • 2
  • 20
  • 29
  • 1
    yup, the way OP wrote this apply, you need `sapply` not `lapply`. `dat[, vec] <- sapply(dat[, vec], function(x) as.numeric(as.character(x)))` works, but it is not as fast to operate on data frames as vectors. – shayaa Jul 23 '16 at 11:19