[R] unexpected results
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 guide says that file.data[var50] 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 can anyone help? Thanks nathan -- View this message in context: http://www.nabble.com/unexpected-results-tf1979786.html#a5432210 Sent from the R help forum at Nabble.com. __ 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.
Re: [R] unexpected results
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[var50] 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.
Re: [R] unexpected results
On Fri, 2006-07-21 at 04:36 -0700, 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 guide says that file.data[var50] 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 can anyone help? Thanks nathan It would appear that your data file is NOT comma delimited, but TAB delimited. The \t characters in the output above support this, since you asked for just the first column for the 5th subject and it appears that you got the entire row. Re-run the import using: jacs.data - read.table(/Users/natha/Desktop/JACSdata2.txt, header=TRUE, sep = \t) so that you are indicating that the delimiter is a TAB character, not a comma. HTH, Marc Schwartz __ 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.
Re: [R] unexpected results
Hi On 21 Jul 2006 at 4:36, nathan wrote: Date sent: Fri, 21 Jul 2006 04:36:53 -0700 (PDT) From: nathan [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Subject:[R] unexpected results 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 I do not think so. Probably separation character of your file is not , as you set in sep=,. don't seem right: The guide says that names(file.data) will give output like ID JURIS RESCODE , but I get ID.JURIS.RESCODE. e.g. ID.JURIS.RESCODE was read into one column. Best way how to copy data from Excel (if you have Excel) Select your data in Excel including first row with headers Ctrl-C Go to R Write mydata - read.delim(clipboard) or look at JACSdata2.txt what is the separator between fields and set it in your read.table command accordingly. From later I suppose your txt file is tab delimited so read.delim() shall capture it. HTH Petr 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 guide says that file.data[var50] 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 can anyone help? Thanks nathan -- View this message in context: http://www.nabble.com/unexpected-results-tf1979786.html#a5432210 Sent from the R help forum at Nabble.com. __ 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. Petr Pikal [EMAIL PROTECTED] __ 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.
Re: [R] unexpected results
Thanks, You are right about it being a tab delimited file - I should have spotted that. I am now getting an error saying that line 4 did not have 27 elements but will fiddle around and try to work it out - I'm guessing because I have some empty feild its causing problems. Anyway thanks for the differnt bits of help -- View this message in context: http://www.nabble.com/unexpected-results-tf1979786.html#a5441821 Sent from the R help forum at Nabble.com. __ 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.
[R] Unexpected results from sort function when partial and index are used
Hi, Consider the following example: sort(10:1, partial=3) ## 1 2 3 7 6 5 4 8 9 10 sort(10:1, index=T) ## $x: 1 2 3 4 5 6 7 8 9 10 ## $ix: 10 9 8 7 6 5 4 3 2 1 sort(10:1, partial=3, index=T) ## 1 2 3 7 6 5 4 8 9 10 The first 2 calls gave expected returns; however, the third one did not returned an index as requested. I could not find anything about it in http://stat.ethz.ch/R-manual/R-patched/library/base/html/sort.html http://stat.ethz.ch/R-manual/R-patched/library/base/html/sort.html , so it seems to be an undocumented feature. Does any body know how to convince sort to return index of partially sorted array? Thanks Jarek =\ Jarek Tuszynski, PhD. o / \ Science Applications International Corporation \__,| (703) 676-4192 \ [EMAIL PROTECTED] `\ [[alternative HTML version deleted]] __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Unexpected results from sort function when partial and index are used
Tuszynski, Jaroslaw W. wrote: Hi, Consider the following example: sort(10:1, partial=3) ## 1 2 3 7 6 5 4 8 9 10 sort(10:1, index=T) ## $x: 1 2 3 4 5 6 7 8 9 10 ## $ix: 10 9 8 7 6 5 4 3 2 1 sort(10:1, partial=3, index=T) ## 1 2 3 7 6 5 4 8 9 10 The first 2 calls gave expected returns; however, the third one did not returned an index as requested. I could not find anything about it in http://stat.ethz.ch/R-manual/R-patched/library/base/html/sort.html http://stat.ethz.ch/R-manual/R-patched/library/base/html/sort.html , so it seems to be an undocumented feature. Does any body know how to convince sort to return index of partially sorted array? Thanks Jarek Jarek, Looking at the code for sort, we see the following: if (!is.null(partial)) { if (!all(is.finite(partial))) stop(non-finite `partial') y - .Internal(psort(x, partial)) } else { # other sort code } so index.return is ignored if partial is provided. To get the index you can use ?match: z - rnorm(10) x - sort(z, partial = 3) ix - match(z, x) Hopefully, I used ?match correctly. Please verify on your own. --sundar __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Unexpected results from sort function when partial and index are used
On Wed, 3 Nov 2004, Tuszynski, Jaroslaw W. wrote: Hi, Consider the following example: sort(10:1, partial=3) ## 1 2 3 7 6 5 4 8 9 10 sort(10:1, index=T) ## $x: 1 2 3 4 5 6 7 8 9 10 ## $ix: 10 9 8 7 6 5 4 3 2 1 sort(10:1, partial=3, index=T) ## 1 2 3 7 6 5 4 8 9 10 The first 2 calls gave expected returns; however, the third one did not returned an index as requested. I could not find anything about it in http://stat.ethz.ch/R-manual/R-patched/library/base/html/sort.html http://stat.ethz.ch/R-manual/R-patched/library/base/html/sort.html , so it seems to be an undocumented feature. Does any body know how to convince sort to return index of partially sorted array? You cannot. There is no underlying code to do so, and the person who added 'index.return' forgot this case. I was against having it at all -- we have sort.list for that purpose. I've updated the documentation. Sundar's match() solution will not work if there are duplicate values. If you need the index, just do a full sort -- partial sorting is only implemented for efficiency reasons, and nowadays full sorting is fast enough even on massive vectors. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html