[R] data frame output in loops
I know this is very basic--I'm wondering if there is a way to write data frames as outputs from a loop. In other words, take this simple example: a - data.frame(x = c(1,2,3,4), y = c(1,2,1,2)) Given a, how would you write a loop that creates two data frames, x and y, where the first column is a column of a and the second column is two times the first column, or: x x.1 x.2 1 1 2 2 2 4 3 3 6 4 4 8 and y y.1 y.2 1 1 2 2 2 4 3 1 2 4 2 4 DISCLAIMER: This e-mail message and any attachments are inte...{{dropped}} __ 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
RE: [R] data frame output in loops
No need for computer-intensive loops. Try the following: a - data.frame(x = c(1,2,3,4), y = c(1,2,1,2)) xx=data.frame(x.1=a[,1],x.2=2*a[,1]) yy=data.frame(y.1=a[,2],y.2=2*a[,2]) Cheers Francisco From: Apoian, Zack [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Subject: [R] data frame output in loops Date: Fri, 6 May 2005 14:56:21 -0400 I know this is very basic--I'm wondering if there is a way to write data frames as outputs from a loop. In other words, take this simple example: a - data.frame(x = c(1,2,3,4), y = c(1,2,1,2)) Given a, how would you write a loop that creates two data frames, x and y, where the first column is a column of a and the second column is two times the first column, or: x x.1 x.2 1 1 2 2 2 4 3 3 6 4 4 8 and y y.1 y.2 1 1 2 2 2 4 3 1 2 4 2 4 DISCLAIMER: This e-mail message and any attachments are inte...{{dropped}} __ 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 __ 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
Re: [R] data frame output
On Thu, 18 Mar 2004, Randy Zelick wrote: Is there a way to *not* supress leading zeros when printing (to the console window or to a file) a dataframe? If you mean via print() or autoprinting, no. I am not sure why you would want to do this, but it seems that using format() and then gsub should work. For example library(MASS) fh - format(hills) fh[] - lapply(fh, function(x) gsub( , 0, as.character(x))) fh dist climbtime Greenmantle 02.5 0650 016.083 Carnethy 06.0 2500 048.350 Craig Dunain 06.0 0900 033.650 Ben Rha 07.5 0800 045.600 Ben Lomond 08.0 3070 062.267 ... See also ?sprintf. -- 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://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] data frame output
Randy == Randy Zelick [EMAIL PROTECTED] on Thu, 18 Mar 2004 21:00:08 -0800 (PST) writes: Randy Hello list, Randy Is there a way to *not* supress leading zeros when printing (to the Randy console window or to a file) a dataframe? Yes, e.g. use formatC(..., format = f) to produce a character matrix and write() that to your file. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] data frame output almost
Hello again, I got three responses for help on the leading zero problem. Thank you. Alas I still don't have it working. Here are more specifics: I read in a data file like this: participants-read.table(C:/Work/blah-blah) The data file consists of the fields last name, first name, social security number, response score 1, response score 2 and so forth. If in the console window I type participants I get something like: Jones Norman 786123344 98.2 16.3 FlintstoneFred 111457654 10.1 8.8 UglyButt 89733456 66.7 32.0 The problem is that the 3rd social security number is really 089733456 and it needs to look like that. None of the methods suggested seemed to work. I could make the social security object alone print with leading zeros, but not as part of the data frame. Thanks again, =Randy= R. Zelick email: [EMAIL PROTECTED] Department of Biology voice: 503-725-3086 Portland State University fax: 503-725-3888 mailing: P.O. Box 751 Portland, OR 97207 shipping: 1719 SW 10th Ave, Room 246 Portland, OR 97201 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] data frame output almost
On Fri, 19 Mar 2004, Randy Zelick wrote: I got three responses for help on the leading zero problem. Thank you. Well, it seems that you didn't tell us what the actual problem was: please consult the posting guide and its references and learn to ask the right question. Alas I still don't have it working. Here are more specifics: I read in a data file like this: participants-read.table(C:/Work/blah-blah) The data file consists of the fields last name, first name, social security number, response score 1, response score 2 and so forth. If in the console window I type participants I get something like: Jones Norman 786123344 98.2 16.3 FlintstoneFred 111457654 10.1 8.8 UglyButt 89733456 66.7 32.0 The problem is that the 3rd social security number is really 089733456 and it needs to look like that. Did you read the help page for read.table or the `R Data Import/Export Manual'? Set colClasses and avoid the conversion to numeric. You didn't tell us you were reading a file with leading zeroes. None of the methods suggested seemed to work. I could make the social security object alone print with leading zeros, but not as part of the data frame. All the solutions I have seen do work, but mine is very simple. You need to convert just that column (which is not what you said you wanted). library(MASS) hills$climb - gsub( , 0, format(hills$climb)) hills You could also use hills$climb - formatC(hills$climb, format=f, flag=0, digits=0, width=4) (Martin didn't give all the details) hills$climb - sprintf(%04.0f, as.double(hills$climb)) In every case, printing the data frame does show leading zeroes. -- 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://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] data frame output
Hello list, Is there a way to *not* supress leading zeros when printing (to the console window or to a file) a dataframe? Thanks, =Randy= R. Zelick email: [EMAIL PROTECTED] Department of Biology voice: 503-725-3086 Portland State University fax: 503-725-3888 mailing: P.O. Box 751 Portland, OR 97207 shipping: 1719 SW 10th Ave, Room 246 Portland, OR 97201 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html