0

I have data in the following form:

time V.out. V.in. Resistor1 Resistor2 Offset
0,00000000000000e+00 -1,498494e-05 0,00000000 10 1M -3
4,91362656238765e-09 3,082234e-02 0,03086832 10 1M -3
4,99663437488765e-09 3,134312e-02 0,03138962 10 1M -3
5,00000000000000e-09 3,136424e-02 0,03141076 10 1M -3
0,00000000000000e+00 -7,485015e-03 0,00000000 10.01K 1M -3
4,90874374988765e-09 7,887614e-03 0,03083766 10.01K 1M -3

The data is read via readr::read_delim() where the columns containing metric prefixes (K = 10^3, M = 10^6 etc.) are read as characters. Is there a way to convert these values to the correct numeric ones? ie.: 10.01K = 10.01 * 10^3or 1M = 1 * 10^6.

Raw Data:

time;V.out.;V.in.;Resistor1;Resistor2;Offset
0,00000000000000e+00;-1,498494e-05;0,00000000;10;1M;-3
4,91362656238765e-09;3,082234e-02;0,03086832;10;1M;-3
4,99663437488765e-09;3,134312e-02;0,03138962;10;1M;-3
5,00000000000000e-09;3,136424e-02;0,03141076;10;1M;-3
0,00000000000000e+00;-7,485015e-03;0,00000000;10.01K;1M;-3
4,90874374988765e-09;7,887614e-03;0,03083766;10.01K;1M;-3
4,99663437488765e-09;8,162769e-03;0,03138962;10.01K;1M;-3
5,00000000000000e-09;8,173305e-03;0,03141076;10.01K;1M;-3
0,00000000000000e+00;-9,966757e-03;0,00000000;20K;1M;-3
IRTFM
  • 258,963
  • 21
  • 364
  • 487
kaos
  • 53
  • 5

1 Answers1

1

This can be approached like this: first, you subset the vector on those values whose last character is K, using sub remove the suffix, convert the result to type numeric with as.numeric, and mutiply it with 10^3. As a final step, you convert the whole vector to numeric.

x[grepl("K$", x)] <- as.numeric(sub("K$", "", x))*10^3
x <- as.numeric(x)
[1]       10 10000000 10010000

And likewise for the suffix M

Data:

x <- c("10", "10.01K", "20K")
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34