An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101007/4ff92925/attachment.pl>
saving object function
6 messages · Joshua Wiley, Joe King
Hi Joe,
You can just put the results into a named list, for example:
functest<-function(x){
a <- x + 1
b <- x + 2
c <- x + 3
results <- list("a" = a, "b" = b, "c" = c)
return(results)
}
functest(1)$a
It is important to name the list or you would have to refer to it as:
functest(1)[[1]] for the first element and so on.
HTH,
Josh
On Thu, Oct 7, 2010 at 11:30 PM, Joe P King <jp at joepking.com> wrote:
Ok so if I have a function:
functest<-function(x){
? ? ? ? ? ?a<-x+1
? ? ? ? ? ?b<-x+2
? ? ? ? ? ?c<-x+3
paste(a)
paste(b)
paste(c)
}
Now I know I can do cat(), or return() on one of them but if I was to run
the function with any number, how could I create objects to save so I could
do, I am wondering if I have more than one object within my function.
functest$a and get the result. I hope this is clear.
-------------------------------------------
Joe King, M.A.
Ph.D. Student
University of Washington - Seattle
206-913-2912
?<mailto:jp at joepking.com> jp at joepking.com
-------------------------------------------
"Never throughout history has a man who lived a life of ease left a name
worth remembering." --Theodore Roosevelt
? ? ? ?[[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.
Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Thank you, well I have a table I am wanting to print, yet I want it in a matrix but it has different number of columns, so if I had a table of
1 1 1
2 2
Is there a way to get that table to print out as an object without having to put an NA or 0 in the last cell?
Joe King
206-913-2912
jp at joepking.com
"Never throughout history has a man who lived a life of ease left a name worth remembering." --Theodore Roosevelt
-----Original Message-----
From: Joshua Wiley [mailto:jwiley.psych at gmail.com]
Sent: Thursday, October 07, 2010 11:43 PM
To: Joe P King
Cc: r-help at r-project.org
Subject: Re: [R] saving object function
Hi Joe,
You can just put the results into a named list, for example:
functest<-function(x){
a <- x + 1
b <- x + 2
c <- x + 3
results <- list("a" = a, "b" = b, "c" = c)
return(results)
}
functest(1)$a
It is important to name the list or you would have to refer to it as:
functest(1)[[1]] for the first element and so on.
HTH,
Josh
On Thu, Oct 7, 2010 at 11:30 PM, Joe P King <jp at joepking.com> wrote:
Ok so if I have a function:
functest<-function(x){
a<-x+1
b<-x+2
c<-x+3
paste(a)
paste(b)
paste(c)
}
Now I know I can do cat(), or return() on one of them but if I was to
run the function with any number, how could I create objects to save
so I could do, I am wondering if I have more than one object within my function.
functest$a and get the result. I hope this is clear.
-------------------------------------------
Joe King, M.A.
Ph.D. Student
University of Washington - Seattle
206-913-2912
<mailto:jp at joepking.com> jp at joepking.com
-------------------------------------------
"Never throughout history has a man who lived a life of ease left a
name worth remembering." --Theodore Roosevelt
[[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.
-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
I did what you said and it worked perfectly, but I have tried to save some of my objects using paste because I want to limit the number of significant digits and one matrix has some empty spaces I use NA in, and I want those left blank, but when I paste into an object it doesn?t hold that formatting, any suggestions? How can I have a matrix that isn?t full to have empty spaces? If I just tell it " ", it turns the entire matrix into characters.
Joe King
206-913-2912
jp at joepking.com
"Never throughout history has a man who lived a life of ease left a name worth remembering." --Theodore Roosevelt
-----Original Message-----
From: Joshua Wiley [mailto:jwiley.psych at gmail.com]
Sent: Thursday, October 07, 2010 11:43 PM
To: Joe P King
Cc: r-help at r-project.org
Subject: Re: [R] saving object function
Hi Joe,
You can just put the results into a named list, for example:
functest<-function(x){
a <- x + 1
b <- x + 2
c <- x + 3
results <- list("a" = a, "b" = b, "c" = c)
return(results)
}
functest(1)$a
It is important to name the list or you would have to refer to it as:
functest(1)[[1]] for the first element and so on.
HTH,
Josh
On Thu, Oct 7, 2010 at 11:30 PM, Joe P King <jp at joepking.com> wrote:
Ok so if I have a function:
functest<-function(x){
a<-x+1
b<-x+2
c<-x+3
paste(a)
paste(b)
paste(c)
}
Now I know I can do cat(), or return() on one of them but if I was to
run the function with any number, how could I create objects to save
so I could do, I am wondering if I have more than one object within my function.
functest$a and get the result. I hope this is clear.
-------------------------------------------
Joe King, M.A.
Ph.D. Student
University of Washington - Seattle
206-913-2912
<mailto:jp at joepking.com> jp at joepking.com
-------------------------------------------
"Never throughout history has a man who lived a life of ease left a
name worth remembering." --Theodore Roosevelt
[[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.
-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Hi Joe,
There is an important distinction between working with data and
presenting data. You do not want to change or get rid of the missing
values in the actual data. Believe it or not, this buys you something
important. Since appearance only matters for presentation, you can
fudge what the data is, and it is fine if it is character data _for
printing_. Here is one possibility where I define a custom printing
function (uninventively, myprinter() ):
# Some data
dat <- matrix(c(1:8, NA), ncol = 3)
# Define a function that removes row and column names, changes NA values to ""
# and prints the matrix without quotes
myprinter <- function(x) {
dat <- x
dimnames(dat) <- list(rep("", nrow(x)), rep("", ncol(x)))
dat[is.na(dat)] <- ""
noquote(format(dat, justify="right"))
}
myprinter(dat)
Ultimately your original data is unchanged, but you get some nice output.
HTH,
Josh
On Fri, Oct 8, 2010 at 6:42 PM, Joe P King <jp at joepking.com> wrote:
I did what you said and it worked perfectly, but I have tried to save some of my objects using paste because I want to limit the number of significant digits and one matrix has some empty spaces I use NA in, and I want those left blank, but when I paste into an object it doesn?t hold that formatting, any suggestions? How can I have a matrix that isn?t full to have empty spaces? If I just tell it " ", it turns the entire matrix into characters.
Joe King
206-913-2912
jp at joepking.com
"Never throughout history has a man who lived a life of ease left a name worth remembering." --Theodore Roosevelt
-----Original Message-----
From: Joshua Wiley [mailto:jwiley.psych at gmail.com]
Sent: Thursday, October 07, 2010 11:43 PM
To: Joe P King
Cc: r-help at r-project.org
Subject: Re: [R] saving object function
Hi Joe,
You can just put the results into a named list, for example:
functest<-function(x){
?a <- x + 1
?b <- x + 2
?c <- x + 3
?results <- list("a" = a, "b" = b, "c" = c)
?return(results)
}
functest(1)$a
It is important to name the list or you would have to refer to it as:
functest(1)[[1]] ?for the first element and so on.
HTH,
Josh
On Thu, Oct 7, 2010 at 11:30 PM, Joe P King <jp at joepking.com> wrote:
Ok so if I have a function:
functest<-function(x){
? ? ? ? ? ?a<-x+1
? ? ? ? ? ?b<-x+2
? ? ? ? ? ?c<-x+3
paste(a)
paste(b)
paste(c)
}
Now I know I can do cat(), or return() on one of them but if I was to
run the function with any number, how could I create objects to save
so I could do, I am wondering if I have more than one object within my function.
functest$a and get the result. I hope this is clear.
-------------------------------------------
Joe King, M.A.
Ph.D. Student
University of Washington - Seattle
206-913-2912
?<mailto:jp at joepking.com> jp at joepking.com
-------------------------------------------
"Never throughout history has a man who lived a life of ease left a
name worth remembering." --Theodore Roosevelt
? ? ? ?[[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.
-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
______________________________________________ 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.
Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
That?s what I was wanting was nice output, I suppose I framed my query wrong, but this is what I wanted! Thank you.
Joe King
206-913-2912
jp at joepking.com
"Never throughout history has a man who lived a life of ease left a name worth remembering." --Theodore Roosevelt
-----Original Message-----
From: Joshua Wiley [mailto:jwiley.psych at gmail.com]
Sent: Friday, October 08, 2010 7:17 PM
To: Joe P King
Cc: r-help at r-project.org
Subject: Re: [R] saving object function
Hi Joe,
There is an important distinction between working with data and presenting data. You do not want to change or get rid of the missing values in the actual data. Believe it or not, this buys you something important. Since appearance only matters for presentation, you can fudge what the data is, and it is fine if it is character data _for printing_. Here is one possibility where I define a custom printing function (uninventively, myprinter() ):
# Some data
dat <- matrix(c(1:8, NA), ncol = 3)
# Define a function that removes row and column names, changes NA values to ""
# and prints the matrix without quotes
myprinter <- function(x) {
dat <- x
dimnames(dat) <- list(rep("", nrow(x)), rep("", ncol(x)))
dat[is.na(dat)] <- ""
noquote(format(dat, justify="right"))
}
myprinter(dat)
Ultimately your original data is unchanged, but you get some nice output.
HTH,
Josh
On Fri, Oct 8, 2010 at 6:42 PM, Joe P King <jp at joepking.com> wrote:
I did what you said and it worked perfectly, but I have tried to save some of my objects using paste because I want to limit the number of significant digits and one matrix has some empty spaces I use NA in, and I want those left blank, but when I paste into an object it doesn?t hold that formatting, any suggestions? How can I have a matrix that isn?t full to have empty spaces? If I just tell it " ", it turns the entire matrix into characters.
Joe King
206-913-2912
jp at joepking.com
"Never throughout history has a man who lived a life of ease left a
name worth remembering." --Theodore Roosevelt
-----Original Message-----
From: Joshua Wiley [mailto:jwiley.psych at gmail.com]
Sent: Thursday, October 07, 2010 11:43 PM
To: Joe P King
Cc: r-help at r-project.org
Subject: Re: [R] saving object function
Hi Joe,
You can just put the results into a named list, for example:
functest<-function(x){
a <- x + 1
b <- x + 2
c <- x + 3
results <- list("a" = a, "b" = b, "c" = c)
return(results)
}
functest(1)$a
It is important to name the list or you would have to refer to it as:
functest(1)[[1]] for the first element and so on.
HTH,
Josh
On Thu, Oct 7, 2010 at 11:30 PM, Joe P King <jp at joepking.com> wrote:
Ok so if I have a function:
functest<-function(x){
a<-x+1
b<-x+2
c<-x+3
paste(a)
paste(b)
paste(c)
}
Now I know I can do cat(), or return() on one of them but if I was to
run the function with any number, how could I create objects to save
so I could do, I am wondering if I have more than one object within my function.
functest$a and get the result. I hope this is clear.
-------------------------------------------
Joe King, M.A.
Ph.D. Student
University of Washington - Seattle
206-913-2912
<mailto:jp at joepking.com> jp at joepking.com
-------------------------------------------
"Never throughout history has a man who lived a life of ease left a
name worth remembering." --Theodore Roosevelt
[[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.
-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
______________________________________________ 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.
-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/