[R] NAs introduced by coercion warning, while plotting data

jim holtman jholtman at gmail.com
Tue Jan 7 16:27:48 CET 2014


You may want to understand how your data is coming out of Excel.  You
are getting just a single character string.  Here is code to convert
the string to numerics and plot:

x <- " 7.0121, -0.673354, 0.749622, -0.549641, 0.435662, -0.328995,
0.0869976, -0.0851428, -0.191019, 0.188799, -0.373707, 0.434814,
-0.51979, 0.61944, -0.554766, 0.662571, -0.557779, 0.543724,
-0.452397, 0.293651, -0.248438, 0.0532787, -0.0185775, -0.176117,
0.222021, -0.34723, 0.407796, -0.416123, 0.497159, -0.435225,
0.473375, -0.370811, 0.33683, -0.273933, 0.149871, -0.142623,
-0.0253748, 0.0205145, -0.134123, 0.167995, -0.209844, 0.266891,
-0.242527, 0.326598, -0.235483, 0.275738, -0.195738, 0.168225,
-0.114333, 0.0593316, -0.0583527, -0.016594, 0.0363428, -0.0533603,
0.106158, -0.129363, 0.131993, -0.134909, 0.135686, -0.125387,
0.0866545, -0.0668686, 0.043161, -0.0229738 ; "
# remove commas and semicolons for reading
x <- gsub(",|;", '', x)
# read in the data
xin <- scan(text = x, what = 0)

plot(xin)

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Tue, Jan 7, 2014 at 10:15 AM, Baro <babakbsn at gmail.com> wrote:
> Thank you sir:
>
> If I am tying
> str(datalist)
>
> I have this output
>
> chr " 7.0121, -0.673354, 0.749622, -0.549641, 0.435662, -0.328995,
> 0.0869976, -0.0851428, -0.191019, 0.188799, -0.373707, 0.434814, "|
> __truncated__
>
>
> and if I try:
> dput(datalist)
>
> I have:
>
> " 7.0121, -0.673354, 0.749622, -0.549641, 0.435662, -0.328995, 0.0869976,
> -0.0851428, -0.191019, 0.188799, -0.373707, 0.434814, -0.51979, 0.61944,
> -0.554766, 0.662571, -0.557779, 0.543724, -0.452397, 0.293651, -0.248438,
> 0.0532787, -0.0185775, -0.176117, 0.222021, -0.34723, 0.407796, -0.416123,
> 0.497159, -0.435225, 0.473375, -0.370811, 0.33683, -0.273933, 0.149871,
> -0.142623, -0.0253748, 0.0205145, -0.134123, 0.167995, -0.209844, 0.266891,
> -0.242527, 0.326598, -0.235483, 0.275738, -0.195738, 0.168225, -0.114333,
> 0.0593316, -0.0583527, -0.016594, 0.0363428, -0.0533603, 0.106158,
> -0.129363, 0.131993, -0.134909, 0.135686, -0.125387, 0.0866545, -0.0668686,
> 0.043161, -0.0229738 ; "
>
>
>
> On Tue, Jan 7, 2014 at 7:11 AM, jim holtman <jholtman at gmail.com> wrote:
>>
>> From your output, it looks like 'datalist' is a character string and
>> not a numeric vector as you expect.  Try doing:
>>
>> str(datalist)
>>
>> to see the structure and to use
>>
>> dput(datalist)
>>
>> to post the data.
>>
>> Jim Holtman
>> Data Munger Guru
>>
>> What is the problem that you are trying to solve?
>> Tell me what you want to do, not how you want to do it.
>>
>>
>> On Tue, Jan 7, 2014 at 10:01 AM, Baro <babakbsn at gmail.com> wrote:
>> > thank you for your answer, I am reading my Data from an excel File. This
>> > is
>> > my R code:
>> >
>> > srow<-2421
>> > wb <- loadWorkbook("C:\\users\\Babak\\Desktop\\spalte205.xls")
>> > dat <-readWorksheet(wb, sheet=getSheets(wb)[1], startRow=srow,
>> > endRow=srow,
>> > startCol=spalte, endCol=spalte,header=FALSE)
>> > datalist<-dat[,1]
>> > datalist<-sub(";","",datalist)
>> > datalist
>> > plot(datalist,type="l",ylim=c(-1,+1))
>> >
>> > as an output of my data, I see:
>> >
>> >> datalist
>> > [1] " 7.0121, -0.673354, 0.749622, -0.549641, 0.435662, -0.328995,
>> > 0.0869976, -0.0851428, -0.191019, 0.188799, -0.373707, 0.434814,
>> > -0.51979,
>> > 0.61944, -0.554766, 0.662571, -0.557779, 0.543724, -0.452397, 0.293651,
>> > -0.248438, 0.0532787, -0.0185775, -0.176117, 0.222021, -0.34723,
>> > 0.407796,
>> > -0.416123, 0.497159, -0.435225, 0.473375, -0.370811, 0.33683, -0.273933,
>> > 0.149871, -0.142623, -0.0253748, 0.0205145, -0.134123, 0.167995,
>> > -0.209844,
>> > 0.266891, -0.242527, 0.326598, -0.235483, 0.275738, -0.195738, 0.168225,
>> > -0.114333, 0.0593316, -0.0583527, -0.016594, 0.0363428, -0.0533603,
>> > 0.106158, -0.129363, 0.131993, -0.134909, 0.135686, -0.125387,
>> > 0.0866545,
>> > -0.0668686, 0.043161, -0.0229738  "
>> >
>> >
>> >
>> >
>> > On Tue, Jan 7, 2014 at 6:54 AM, PIKAL Petr <petr.pikal at precheza.cz>
>> > wrote:
>> >
>> >> Hi
>> >>
>> >> I did not get any error with your data
>> >>
>> >> plot(test)
>> >>
>> >> dput(test)
>> >> c(7.0121, -0.673354, 0.749622, -0.549641, 0.435662, -0.328995,
>> >> 0.0869976, -0.0851428, -0.191019, 0.188799, -0.373707, 0.434814,
>> >> -0.51979, 0.61944, -0.554766, 0.662571, -0.557779, 0.543724,
>> >> -0.452397, 0.293651, -0.248438, 0.0532787, -0.0185775, -0.176117,
>> >> 0.222021, -0.34723, 0.407796, -0.416123, 0.497159, -0.435225,
>> >> 0.473375, -0.370811, 0.33683, -0.273933, 0.149871, -0.142623,
>> >> -0.0253748, 0.0205145, -0.134123, 0.167995, -0.209844, 0.266891,
>> >> -0.242527, 0.326598, -0.235483, 0.275738, -0.195738, 0.168225,
>> >> -0.114333, 0.0593316, -0.0583527, -0.016594, 0.0363428, -0.0533603,
>> >> 0.106158, -0.129363, 0.131993, -0.134909, 0.135686, -0.125387,
>> >> 0.0866545, -0.0668686, 0.043161, -0.0229738)
>> >>
>> >> You shall explain more clearly what you did (code) and what are your
>> >> data
>> >> (dput)
>> >>
>> >> I presume that the story is not as simple as plotting a vector as you
>> >> have
>> >> suggested.
>> >>
>> >> Petr
>> >>
>> >> > -----Original Message-----
>> >> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> >> > project.org] On Behalf Of Baro
>> >> > Sent: Tuesday, January 07, 2014 3:40 PM
>> >> > To: R help
>> >> > Subject: [R] NAs introduced by coercion warning, while plotting data
>> >> >
>> >> > Hi all
>> >> >
>> >> > I have such a Data and I want to plot them but I get this warning
>> >> >
>> >> > Warning message:
>> >> > In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
>> >> >
>> >> >
>> >> > " 7.0121, -0.673354, 0.749622, -0.549641, 0.435662, -0.328995,
>> >> > 0.0869976, -0.0851428, -0.191019, 0.188799, -0.373707, 0.434814, -
>> >> > 0.51979, 0.61944, -0.554766, 0.662571, -0.557779, 0.543724,
>> >> > -0.452397,
>> >> > 0.293651, -0.248438, 0.0532787, -0.0185775, -0.176117, 0.222021, -
>> >> > 0.34723, 0.407796, -0.416123, 0.497159, -0.435225, 0.473375,
>> >> > -0.370811,
>> >> > 0.33683, -0.273933, 0.149871, -0.142623, -0.0253748, 0.0205145, -
>> >> > 0.134123, 0.167995, -0.209844, 0.266891, -0.242527, 0.326598, -
>> >> > 0.235483, 0.275738, -0.195738, 0.168225, -0.114333, 0.0593316, -
>> >> > 0.0583527, -0.016594, 0.0363428, -0.0533603, 0.106158, -0.129363,
>> >> > 0.131993, -0.134909, 0.135686, -0.125387, 0.0866545, -0.0668686,
>> >> > 0.043161, -0.0229738 "
>> >> >
>> >> > Why do I get this warning? how can I solve it?
>> >> >
>> >> >       [[alternative HTML version deleted]]
>> >> >
>> >> > ______________________________________________
>> >> > 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.
>> >>
>> >
>> >         [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > 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.
>
>




More information about the R-help mailing list