Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting string to numeric [duplicate]

Tags:

string

r

I've imported a test file and tried to make a histogram

pichman <- read.csv(file="picman.txt", header=TRUE, sep="/t")    hist <- as.numeric(pichman$WS)     

However, I get different numbers from values in my dataset. Originally I thought that this because I had text, so I deleted the text:

table(pichman$WS)     ws <- pichman$WS[pichman$WS!="Down" & pichman$WS!="NoData"]     

However, I am still getting very high numbers does anyone have an idea?

like image 758
eliavs Avatar asked Feb 08 '11 09:02

eliavs


People also ask

How do I convert string variables to numeric ones?

You can compute a new numeric variable from the original string variable through SPSS command syntax using either of two routes: RECODE strngvar (CONVERT) INTO numvar. COMPUTE numvar=NUMBER(strngvar,Fw. d).

What command is used to change a string into a number?

One way is to use the number function with the compute command. To do this, you need to create a new variable using the compute command. To use the number function, you need to enclose the name of the string variable and a format for the new numeric variable. compute score1 = number(score, F2).


1 Answers

I suspect you are having a problem with factors. For example,

> x = factor(4:8) > x [1] 4 5 6 7 8 Levels: 4 5 6 7 8 > as.numeric(x) [1] 1 2 3 4 5 > as.numeric(as.character(x)) [1] 4 5 6 7 8 

Some comments:

  • You mention that your vector contains the characters "Down" and "NoData". What do expect/want as.numeric to do with these values?
  • In read.csv, try using the argument stringsAsFactors=FALSE
  • Are you sure it's sep="/t and not sep="\t"
  • Use the command head(pitchman) to check the first fews rows of your data
  • Also, it's very tricky to guess what your problem is when you don't provide data. A minimal working example is always preferable. For example, I can't run the command pichman <- read.csv(file="picman.txt", header=TRUE, sep="/t") since I don't have access to the data set.
like image 149
csgillespie Avatar answered Sep 24 '22 02:09

csgillespie