Convert string to variable in R
I'm trying to read data in R from a text file so that I can build it:
coupling <- read.table("~/table.format",stringsAsFactors = FALSE, sep='\t')
A line from this table looks like this:
133 0.0116, 0.0226, 0.0236, 0.0244, 0.0264, 0.0124, 0.013, 0.014, 0.0158, 0.034, 0.0348, 0.0356, 0.0372 329777.0, -236464.0, -348470.0, -554708.0, -471896.0, 538782.0, 695291.0, 812729.0, 983141.0, 208212.0, 214012.0, 366636.0, 343232.0
If the columns (remainder, delay, height) are separated by tabs, and the data in the columns is separated by ",". Now I would like to plot the height versus delay, so I am trying to assign variables to the columns:
xdata <- c(coupling[1,2])
ydata <- c(coupling[1,3])
However, if I try to plot a graph (xdata, ydata), I get the following errors:
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
5: In min(x) : no non-missing arguments to min; returning Inf
6: In max(x) : no non-missing arguments to max; returning -Inf
Printing xdata (and ydata) gives a variable of the form:
xdata
[1] "0.0116, 0.0226, 0.0236, 0.0244, 0.0264, 0.0124, 0.013, 0.014, 0.0158, 0.034, 0.0348, 0.0356, 0.0372 "
Presumably R cannot plot this using quotes. I tried several alternatives to try and work around this, however none of them worked:
newxdata <-as.numeric(xdata)
Returns an error:
Warning message:
NAs introduced by coercion
The seal closed me:
print(xdata,quote=FALSE)
It looks like a trick; the output loses the quotes:
[1] 0.0116, 0.0226, 0.0236, 0.0244, 0.0264, 0.0124, 0.013, 0.014, 0.0158, 0.034, 0.0348, 0.0356, 0.0372
But if I assign it to a variable, the quotes reappear and I still can't plot the data:
newxdata <- c(print(xdata,quote=FALSE))
newxdata
[1] "0.0116, 0.0226, 0.0236, 0.0244, 0.0264, 0.0124, 0.013, 0.014, 0.0158, 0.034, 0.0348, 0.0356, 0.0372 "
How can I get around this problem?
source to share
You need some changes first and then it will work. The reason for the quotes is because you have a character vector of length 1 that you need to convert to a numeric vector of length 13:
#initial data set: character vector of length 1
a <- "0.0116, 0.0226, 0.0236, 0.0244, 0.0264, 0.0124, 0.013, 0.014, 0.0158, 0.034, 0.0348, 0.0356, 0.0372 "
#function to trim leading and trailing spaces **see bottom of answer
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
#first use strsplit to split the long string into separate string elements
#that are comma separated.
#Then use trim on each element to remove leading and trailing spaces
b <- trim(strsplit(a, ',')[[1]])
#finally use as.numeric to convert to numbers
c <- as.numeric(b)
Variable c can now be used in graphics
Output:
> c
[1] 0.0116 0.0226 0.0236 0.0244 0.0264 0.0124 0.0130 0.0140 0.0158 0.0340 0.0348 0.0356 0.0372
The function trim
was taken from here
EDIT
Apparently, as per @ zero323's comment, you don't even need to trim the character vector. So this works great in one call:
> as.numeric(strsplit(a, ',')[[1]])
[1] 0.0116 0.0226 0.0236 0.0244 0.0264 0.0124 0.0130 0.0140 0.0158 0.0340 0.0348 0.0356 0.0372
source to share
You can also use scan
(data from @LyzandeR post)
scan(text=a, what=numeric(), sep=",", quiet=TRUE)
#[1] 0.0116 0.0226 0.0236 0.0244 0.0264 0.0124 0.0130 0.0140 0.0158 0.0340
#[11] 0.0348 0.0356 0.0372
You can directly use scan
to read from a file withsep=","
scan("~/table.format", what=numeric(), sep=",", quiet=TRUE) #not tested
source to share