Skip to content

Why Does R Print out strange record?

2 messages · arthur brogard, Richard M. Heiberger

#
I have this bit of code:
rates=read.csv("Rates2.csv")
attach(rates)
mysize <- nrow(rates)
count <- 0
for(i in 1:(mysize - 3)) {
#print(i)
thisday <- Date[i]
thisone <- Int[i+1] - Int[i]
nextone <- Int[i+2] - Int[i]
lastone <- thisone + nextone
lastone <- lastone/6.5
lastone <- lastone * 1000

#print("thisone")
#print(thisone)
#print("nextone")
#print(nextone)

if (lastone >0){
print(thisday)
print("dollars saved per $100,000")
print(lastone)
count = count + 1
}
}
which generally works alright until I put the last bit in about 'thisday'
since then I get a display with such as this in it:
[1] "dollars saved per $100,000"
[1] 9.230769
[1] Feb-2012
689 Levels: Apr-1959 Apr-1960 Apr-1961 Apr-1962 ... Sep-2015
[1] "dollars saved per $100,000"
[1] 18.46154
[1] Jun-2015
689 Levels: Apr-1959 Apr-1960 Apr-1961 Apr-1962 ... Sep-2015
[1] "dollars saved per $100,000"
[1] 21.53846

This word 'Levels'? is not present in the data.? 

Can anyone help, tell me what is happening here?


?================================

Can you handle truth ? 

Barbara Walker: Woman's Encyclopedia of Myths and Secrets

What it is really about:
https://www.youtube.com/watch?v=sYKAgAfYJZE

The bald truth:
https://www.youtube.com/watch?v=kevNDOCbKxE

https://www.youtube.com/watch?v=fbfdsMdbcfA

Sanity:
Those terrible Russians:
https://www.youtube.com/watch?v=VZsjoiFfpXM
#
You have read your Date variable in as a character variable, which was
coerced to a factor.
You probably wanted the values to be interpreted as dates.  example of
what to do is below.
Note that the date-factor levels are sorted alphabetically, which is
almost certainly not what you want.

In addition, please do not use HTML mail.  R-help is plain-text list
and R code often gets scrambled beyond recognition.

Attaching a data.frame is very bad style, and will get you into
trouble in the future.  Again, see below for a recommendation.



rates <- read.csv(text="
    Date, Int
Feb-2012, .04
Mar-2012, .045
Apr-2012, .042
May-2012, .038
Jun-2012, .051
",
header=TRUE,
colClasses=c("character","numeric"))

rates
sapply(rates, class)


## dates are one of the most difficult computing concepts to get
## right.  your dates are month and year, not including day-of-month,
## and are therefore even more difficult.  I am putting them all on
## the first of the month, and forcing a common time zone to bypass
## the daylight savings time problems.

rates$Date <- strptime(paste0("1-", rates$Date), format="%d-%b-%Y", tz="UTC")
## you will need to read
## ?as.POSIXct
## ?strptime

## the rest of your calculations can be done more easily with
vectorized arithmetic.

mysize <- nrow(rates)
rates$thisone <- c(diff(rates$Int), NA)
rates$nextone <- c(diff(rates$Int, lag=2), NA, NA)
rates$lastone <- (rates$thisone + rates$nextone)/6.5*1000
rates

On Mon, Dec 19, 2016 at 8:08 PM, arthur brogard via R-help
<r-help at r-project.org> wrote: