Re: [R] unexpected results

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Fri 21 Jul 2006 - 22:23:00 EST

On 7/21/2006 7:36 AM, nathan wrote:
> Hi,
>
> I'm just learning R and am encountering some unexpected results following a
> guide on the internet. If anyone can help that would be great - I think it
> is something about the way the data has been read in!
>
> I've read a coma delimited text data file that was saved from excel:
>

>> jacs.data <- read.table("/Users/natha/Desktop/JACSdata2.txt", header=TRUE,
>> sep=",")

>
> This seemed to work fine, but then I start to get unusual results that don't
> seem right:
>
> The guide says that names(file.data) will give output like "ID" "JURIS"
> "RESCODE" , but I get ID.JURIS.RESCODE.
>
> The guide says that file.data[5,1] will give me the data for the 5th subject
> but i get:
> [1] 7\tACT\t\t\tSUMMONS\tACTCC321.001\tA.C.T. - MINOR THEFT (REPLACEMENT
> VALUE $2000 OR LESS)\ etc - which seems scrambled

The "\t" values are tabs. I think your file was tab delimited, not comma delimited. R thinks it has only one column, because it didn't fine any commas.
>
> The guide says that file.data[var5>0] will give me the data for all subject
> who meet that condition (ie greater than 0 on var5), but I get:
>
> Error in "[.data.frame"(jacs.data, offend > 0) :
> object "offend" not found

It looks like you typed jacs.data[offend > 0]. There are two problems:

  1. You want to select rows matching the condition, so you need another comma, i.e.

jacs.data[offend > 0, ]

(the empty entry after the comma means "all columns").

2. You need to have a variable named offend outside the dataframe. The error message makes it look as though you don't.

If offend is a column in the dataframe, then you would use

jacs.data[jacs.data$offend > 0, ]

or

subset(jacs.data, offend > 0)

Duncan Murdoch
> can anyone help? Thanks nathan



R-help@stat.math.ethz.ch 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. Received on Fri Jul 21 22:27:59 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 22 Jul 2006 - 00:16:13 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.