destring
Turns a column of text that is really numbers into actual numbers.
What we have
generate()orreplaceignore(),force,percent,floatanddpcomma
Coming later
Nothing known to be missing.
How you write it
destring varlist, {replace | generate(newvars)} [ignore(",") force percent float]
Square brackets mean optional. A vertical bar means choose one.
What it does
Spreadsheets produce these constantly: a column that reads 1,250 or 45% or 37 and arrives as text, so it will not average. destring converts it.
ignore(",") strips the thousands separators, percent handles the sign, and force blanks out anything that still is not a number rather than refusing the whole column.
Watch out. Use
force only after you have looked at what it will blank out — it silently turns 'refused' and 'n/a' into missing values, which may or may not be what you want.Try it
Every example below was run after sysuse vira.sample02, clear.
. tostring age, generate(age_text)
age: generated as str
The same thing in pandas and in R
R
# no R form for `tostring` yet -- see docs/r-equivalents.md
. destring age_text, generate(age_again)
age_text: all characters numeric; generated as byte
The same thing in pandas and in R
pandas
import pandas as pd df[['age_again']] = df[['age_text']].apply(pd.to_numeric, errors="coerce")
R
# no R form for `destring` yet -- see docs/r-equivalents.md
. describe age_text age_again
Contains data from .../virata/datasets/vira.sample02.dta
Observations: 6,000
Variables: 2
------------------------------------------------------------------------------
Variable Storage Display Value
name type format label Variable label
------------------------------------------------------------------------------
age_text str %9s
age_again byte %9.0g
------------------------------------------------------------------------------
The same thing in pandas and in R
pandas
df.dtypes
R
library(dplyr) glimpse(df) # This R has NOT been run against a real R yet: read it as documentation.