[R] how to ignore or omit somethings while reading the data table

jim holtman jholtman at gmail.com
Wed Jan 2 14:35:10 CET 2008


Here is a way of doing it:

> x <- "1 2 $.05
+ 3 4 $1.05
+ 5 6 $23.56"
> x <- read.table(textConnection(x))
> x
  V1 V2     V3
1  1  2   $.05
2  3  4  $1.05
3  5  6 $23.56
> # delete "$" and convert to numeric
> x$converted <- as.numeric(sub("\\$", "", as.character(x$V3)))
> str(x)
'data.frame':   3 obs. of  4 variables:
 $ V1       : int  1 3 5
 $ V2       : int  2 4 6
 $ V3       : Factor w/ 3 levels "$.05","$1.05",..: 1 2 3
 $ converted: num   0.05  1.05 23.56
> x
  V1 V2     V3 converted
1  1  2   $.05      0.05
2  3  4  $1.05      1.05
3  5  6 $23.56     23.56


On Jan 2, 2008 8:14 AM, mrafi <confessin at gmail.com> wrote:
>
> hello respected ppl...
> am a engg. student...i was trying to use R in statistical calculations
> now the problem is..i imported a huge tsv file onto R...it has a column
> which gives cost...and it has "$" with each numerical value in this
> column...it is something like this..$.05,$.1,$.075...and so on..
> R is reading it as "character vector"...
> i tried using all the arguments but could only change it to a "factor"
> i want to read it as numericals...and perform further operations..
> i know it is a stupid problem but can any1 help me get outta this...
> thank you all...!!!
> --
> View this message in context: http://www.nabble.com/how-to-ignore-or-omit-somethings-while-reading-the-data-table-tp14578131p14578131.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?




More information about the R-help mailing list