schande
schande

Reputation: 658

How to convert all values in a column to floating point values in R?

I have a dataframe like this:

ID     A     B     
 0    0052  225  
 1   00558  305  
 2    0855  250  
...              ...

All I want is to change the values of column 'A' in a way, that it looks like this, with only the last two digits as floating point values and the third last in front of the decimal point:

ID    A     B    
 0   0.52  225  
 1   5.58  305  
 2   8.55  250  
...             ...




I am new to R, if you know some good basic guidelines or books for scientific plotting, let me know. :-)

Thanks for your help.

Upvotes: 0

Views: 5217

Answers (1)

Eric Fail
Eric Fail

Reputation: 7948

more less coping nicola's comments to provide a complete minimal reproducible example to go along with your question and to enable the question to be closed.

df <- structure(list(ID = 0:2, A = c('0052', '00558', '0855'), B = c(225L, 
305L, 250L)), .Names = c("ID", "A", "B"), class = "data.frame", row.names = c(NA, 
-3L))
df
#>   ID     A   B
#> 1  0  0052 225
#> 2  1 00558 305
#> 3  2  0855 250
str(df)
#> 'data.frame':    3 obs. of  3 variables:
#>  $ ID: int  0 1 2
#>  $ A : chr  "0052" "00558" "0855"
#>  $ B : int  225 305 250
df$A <- as.numeric(df$A)/100
str(df)
#> 'data.frame':    3 obs. of 
#>  $ ID: int  0 1 2
#>  $ A : num  0.52 5.58 8.55
#>  $ B : int  225 305 250
df
#>   ID    A   B
#> 1  0 0.52 225
#> 2  1 5.58 305
#> 3  2 8.55 250

Upvotes: 1

Related Questions