Why do R objects not print in a function or a “for” loop?2019 Community Moderator Electionfor loop in r not workingwriting file with sink in RFor loop not printing in RI'm trying to write a for loop but one of the values won't update. RNested for loops isn't working in RR: Outputting API obtained environment through loopR: Using for-loop and filter data.tableNAs introduced by coercion warning, and results of if block not printingPlot inside a loop using highcharterApplying a loop to a function in RHow to flush output of print function?What's the simplest way to print a Java array?Why is using “for…in” with array iteration a bad idea?A 'for' loop to iterate over an enum in JavaLoop through an array in JavaScriptGrouping functions (tapply, by, aggregate) and the *apply familyHow to print to stderr in Python?Why is `[` better than `subset`?How can I view the source code for a function?Why is printing “B” dramatically slower than printing “#”?
Does splitting a potentially monolithic application into several smaller ones help prevent bugs?
Provisioning profile doesn't include the application-identifier and keychain-access-groups entitlements
Making a sword in the stone, in a medieval world without magic
How does Dispel Magic work against Stoneskin?
Can "semicircle" be used to refer to a part-circle that is not a exact half-circle?
Single word request: Harming the benefactor
Need some help with my first LaTeX drawing…
Am I not good enough for you?
When two POV characters meet
Is going from continuous data to categorical always wrong?
Unreachable code, but reachable with exception
What to do when during a meeting client people start to fight (even physically) with each others?
Why doesn't the EU now just force the UK to choose between referendum and no-deal?
What is the blue range indicating on this manifold pressure gauge?
What is the likely impact on flights of grounding an entire aircraft series?
Do Bugbears' arms literally get longer when it's their turn?
Is it ok to include an epilogue dedicated to colleagues who passed away in the end of the manuscript?
Question about partial fractions with irreducible quadratic factors
Is it true that real estate prices mainly go up?
Best approach to update all entries in a list that is paginated?
Extension of Splitting Fields over An Arbitrary Field
What does おとこえしや mean?
Word for a person who has no opinion about whether god exists
"One can do his homework in the library"
Why do R objects not print in a function or a “for” loop?
2019 Community Moderator Electionfor loop in r not workingwriting file with sink in RFor loop not printing in RI'm trying to write a for loop but one of the values won't update. RNested for loops isn't working in RR: Outputting API obtained environment through loopR: Using for-loop and filter data.tableNAs introduced by coercion warning, and results of if block not printingPlot inside a loop using highcharterApplying a loop to a function in RHow to flush output of print function?What's the simplest way to print a Java array?Why is using “for…in” with array iteration a bad idea?A 'for' loop to iterate over an enum in JavaLoop through an array in JavaScriptGrouping functions (tapply, by, aggregate) and the *apply familyHow to print to stderr in Python?Why is `[` better than `subset`?How can I view the source code for a function?Why is printing “B” dramatically slower than printing “#”?
I have an R matrix named ddd. When I enter this, everything works fine:
i <- 1
shapiro.test(ddd[,y])
ad.test(ddd[,y])
stem(ddd[,y])
print(y)
The calls to Shapiro Wilk, Anderson Darling, and stem all work, and extract the same column.
If I put this code in a "for" loop, the calls to Shapiro Wilk, and Anderson Darling stop working, while the the stem & leaf call and the print call continue to work.
for (y in 7:10)
shapiro.test(ddd[,y])
ad.test(ddd[,y])
stem(ddd[,y])
print(y)
The decimal point is 1 digit(s) to the right of the |
0 | 0
0 | 899999
1 | 0
[1] 7
The same thing happens if I try and write a function. SW & AD do not work. The other calls do.
> D <- function (y)
+ shapiro.test(ddd[,y])
+ ad.test(ddd[,y])
+ stem(ddd[,y])
+ print(y)
> D(9)
The decimal point is at the |
9 | 000
9 |
10 | 00000
[1] 9
Why don't all the calls behave the same way?
r for-loop printing r-faq
add a comment |
I have an R matrix named ddd. When I enter this, everything works fine:
i <- 1
shapiro.test(ddd[,y])
ad.test(ddd[,y])
stem(ddd[,y])
print(y)
The calls to Shapiro Wilk, Anderson Darling, and stem all work, and extract the same column.
If I put this code in a "for" loop, the calls to Shapiro Wilk, and Anderson Darling stop working, while the the stem & leaf call and the print call continue to work.
for (y in 7:10)
shapiro.test(ddd[,y])
ad.test(ddd[,y])
stem(ddd[,y])
print(y)
The decimal point is 1 digit(s) to the right of the |
0 | 0
0 | 899999
1 | 0
[1] 7
The same thing happens if I try and write a function. SW & AD do not work. The other calls do.
> D <- function (y)
+ shapiro.test(ddd[,y])
+ ad.test(ddd[,y])
+ stem(ddd[,y])
+ print(y)
> D(9)
The decimal point is at the |
9 | 000
9 |
10 | 00000
[1] 9
Why don't all the calls behave the same way?
r for-loop printing r-faq
what isi
for - did you meany <- 1
in the first line?
– Gavin Simpson
Jan 17 '11 at 18:13
add a comment |
I have an R matrix named ddd. When I enter this, everything works fine:
i <- 1
shapiro.test(ddd[,y])
ad.test(ddd[,y])
stem(ddd[,y])
print(y)
The calls to Shapiro Wilk, Anderson Darling, and stem all work, and extract the same column.
If I put this code in a "for" loop, the calls to Shapiro Wilk, and Anderson Darling stop working, while the the stem & leaf call and the print call continue to work.
for (y in 7:10)
shapiro.test(ddd[,y])
ad.test(ddd[,y])
stem(ddd[,y])
print(y)
The decimal point is 1 digit(s) to the right of the |
0 | 0
0 | 899999
1 | 0
[1] 7
The same thing happens if I try and write a function. SW & AD do not work. The other calls do.
> D <- function (y)
+ shapiro.test(ddd[,y])
+ ad.test(ddd[,y])
+ stem(ddd[,y])
+ print(y)
> D(9)
The decimal point is at the |
9 | 000
9 |
10 | 00000
[1] 9
Why don't all the calls behave the same way?
r for-loop printing r-faq
I have an R matrix named ddd. When I enter this, everything works fine:
i <- 1
shapiro.test(ddd[,y])
ad.test(ddd[,y])
stem(ddd[,y])
print(y)
The calls to Shapiro Wilk, Anderson Darling, and stem all work, and extract the same column.
If I put this code in a "for" loop, the calls to Shapiro Wilk, and Anderson Darling stop working, while the the stem & leaf call and the print call continue to work.
for (y in 7:10)
shapiro.test(ddd[,y])
ad.test(ddd[,y])
stem(ddd[,y])
print(y)
The decimal point is 1 digit(s) to the right of the |
0 | 0
0 | 899999
1 | 0
[1] 7
The same thing happens if I try and write a function. SW & AD do not work. The other calls do.
> D <- function (y)
+ shapiro.test(ddd[,y])
+ ad.test(ddd[,y])
+ stem(ddd[,y])
+ print(y)
> D(9)
The decimal point is at the |
9 | 000
9 |
10 | 00000
[1] 9
Why don't all the calls behave the same way?
r for-loop printing r-faq
r for-loop printing r-faq
edited Nov 9 '14 at 14:12
Richie Cotton
80.5k28189308
80.5k28189308
asked Jan 17 '11 at 17:49
Sal LeggioSal Leggio
261133
261133
what isi
for - did you meany <- 1
in the first line?
– Gavin Simpson
Jan 17 '11 at 18:13
add a comment |
what isi
for - did you meany <- 1
in the first line?
– Gavin Simpson
Jan 17 '11 at 18:13
what is
i
for - did you mean y <- 1
in the first line?– Gavin Simpson
Jan 17 '11 at 18:13
what is
i
for - did you mean y <- 1
in the first line?– Gavin Simpson
Jan 17 '11 at 18:13
add a comment |
3 Answers
3
active
oldest
votes
In a loop, automatic printing is turned off, as it is inside a function. You need to explicitly print
something in both cases if you want to see the output. The [1] 9
things you are getting is because you are explicitly printing the values of y
.
Here is an example of how you might want to consider going about doing this.
> DF <- data.frame(A = rnorm(100), B = rlnorm(100))
> y <- 1
> shapiro.test(DF[,y])
Shapiro-Wilk normality test
data: DF[, y]
W = 0.9891, p-value = 0.5895
So we have automatic printing. In the loop we would have to do this:
for(y in 1:2)
print(shapiro.test(DF[,y]))
If you want to print more tests out, then just add them as extra lines in the loop:
for(y in 1:2)
writeLines(paste("Shapiro Wilks Test for column", y))
print(shapiro.test(DF[,y]))
writeLines(paste("Anderson Darling Test for column", y))
print(ad.test(DF[,y]))
But that isn't very appealing unless you like reading through reams of output. Instead, why not save the fitted test objects and then you can print them and investigate them, maybe even process them to aggregate the test statistics and p-values into a table? You can do that using a loop:
## object of save fitted objects in
obj <- vector(mode = "list", length = 2)
## loop
for(y in seq_along(obj))
obj[[y]] <- shapiro.test(DF[,y])
We can then look at the models using
> obj[[1]]
Shapiro-Wilk normality test
data: DF[, y]
W = 0.9891, p-value = 0.5895
for example, or using lapply
, which takes care of setting up the object we use to store the results for us:
> obj2 <- lapply(DF, shapiro.test)
> obj2[[1]]
Shapiro-Wilk normality test
data: X[[1L]]
W = 0.9891, p-value = 0.5895
Say now I wanted to extract the W
and p-value
data, we can process the object storing all the results to extract the bits we want, e.g.:
> tab <- t(sapply(obj2, function(x) c(x$statistic, x$p.value)))
> colnames(tab) <- c("W", "p.value")
> tab
W p.value
A 0.9890621 5.894563e-01
B 0.4589731 1.754559e-17
Or for those with a penchant for significance stars:
> tab2 <- lapply(obj2, function(x) c(W = unname(x$statistic),
+ `p.value` = x$p.value))
> tab2 <- data.frame(do.call(rbind, tab2))
> printCoefmat(tab2, has.Pvalue = TRUE)
W p.value
A 0.9891 0.5895
B 0.4590 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
This has got to be better than firing output to the screen that you then have to pour through?
Thanks a lot, Gavin. I did not know that "print" was turned off in loops and functions. The doc didn't say that. I was also unfamiliar with "y in seq_along(obj)". Didn't know you could do that! Your answer is very helpful.
– Sal Leggio
Jan 21 '11 at 3:36
I didn't know about 'printCoefmat'. Great!!
– Rodrigo
Mar 29 '16 at 18:12
add a comment |
Not a new answer, but in addition to the above: "flush.console()" is necessary to force printing to take place DURING the loop rather than after. Only reason I use print() during a loop is to show progress, e.g., of reading many files.
for (i in 1:10)
print(i)
flush.console()
for(j in 1:100000)
k <- 0
Up vote for answering the question stated in the title. This should be edited thus the title of this questions does not match the question itself.
– aldux
Oct 25 '12 at 15:27
Up vote for flush.console()
– szeta
Jul 29 '15 at 10:07
add a comment |
Fantastic answer from Gavin Simpson. I took the last bit of magic and turned it into a function.
sw.df <- function ( data )
obj <- lapply(data, shapiro.test)
tab <- lapply(obj, function(x) c(W = unname(x$statistic), `p.value` = x$p.value))
tab <- data.frame(do.call(rbind, tab))
printCoefmat(tab, has.Pvalue = TRUE)
Then you can just call it with your data frame
sw.df ( df )
And if you want to try a transformation:
sw.df ( log(df) )
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4716152%2fwhy-do-r-objects-not-print-in-a-function-or-a-for-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In a loop, automatic printing is turned off, as it is inside a function. You need to explicitly print
something in both cases if you want to see the output. The [1] 9
things you are getting is because you are explicitly printing the values of y
.
Here is an example of how you might want to consider going about doing this.
> DF <- data.frame(A = rnorm(100), B = rlnorm(100))
> y <- 1
> shapiro.test(DF[,y])
Shapiro-Wilk normality test
data: DF[, y]
W = 0.9891, p-value = 0.5895
So we have automatic printing. In the loop we would have to do this:
for(y in 1:2)
print(shapiro.test(DF[,y]))
If you want to print more tests out, then just add them as extra lines in the loop:
for(y in 1:2)
writeLines(paste("Shapiro Wilks Test for column", y))
print(shapiro.test(DF[,y]))
writeLines(paste("Anderson Darling Test for column", y))
print(ad.test(DF[,y]))
But that isn't very appealing unless you like reading through reams of output. Instead, why not save the fitted test objects and then you can print them and investigate them, maybe even process them to aggregate the test statistics and p-values into a table? You can do that using a loop:
## object of save fitted objects in
obj <- vector(mode = "list", length = 2)
## loop
for(y in seq_along(obj))
obj[[y]] <- shapiro.test(DF[,y])
We can then look at the models using
> obj[[1]]
Shapiro-Wilk normality test
data: DF[, y]
W = 0.9891, p-value = 0.5895
for example, or using lapply
, which takes care of setting up the object we use to store the results for us:
> obj2 <- lapply(DF, shapiro.test)
> obj2[[1]]
Shapiro-Wilk normality test
data: X[[1L]]
W = 0.9891, p-value = 0.5895
Say now I wanted to extract the W
and p-value
data, we can process the object storing all the results to extract the bits we want, e.g.:
> tab <- t(sapply(obj2, function(x) c(x$statistic, x$p.value)))
> colnames(tab) <- c("W", "p.value")
> tab
W p.value
A 0.9890621 5.894563e-01
B 0.4589731 1.754559e-17
Or for those with a penchant for significance stars:
> tab2 <- lapply(obj2, function(x) c(W = unname(x$statistic),
+ `p.value` = x$p.value))
> tab2 <- data.frame(do.call(rbind, tab2))
> printCoefmat(tab2, has.Pvalue = TRUE)
W p.value
A 0.9891 0.5895
B 0.4590 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
This has got to be better than firing output to the screen that you then have to pour through?
Thanks a lot, Gavin. I did not know that "print" was turned off in loops and functions. The doc didn't say that. I was also unfamiliar with "y in seq_along(obj)". Didn't know you could do that! Your answer is very helpful.
– Sal Leggio
Jan 21 '11 at 3:36
I didn't know about 'printCoefmat'. Great!!
– Rodrigo
Mar 29 '16 at 18:12
add a comment |
In a loop, automatic printing is turned off, as it is inside a function. You need to explicitly print
something in both cases if you want to see the output. The [1] 9
things you are getting is because you are explicitly printing the values of y
.
Here is an example of how you might want to consider going about doing this.
> DF <- data.frame(A = rnorm(100), B = rlnorm(100))
> y <- 1
> shapiro.test(DF[,y])
Shapiro-Wilk normality test
data: DF[, y]
W = 0.9891, p-value = 0.5895
So we have automatic printing. In the loop we would have to do this:
for(y in 1:2)
print(shapiro.test(DF[,y]))
If you want to print more tests out, then just add them as extra lines in the loop:
for(y in 1:2)
writeLines(paste("Shapiro Wilks Test for column", y))
print(shapiro.test(DF[,y]))
writeLines(paste("Anderson Darling Test for column", y))
print(ad.test(DF[,y]))
But that isn't very appealing unless you like reading through reams of output. Instead, why not save the fitted test objects and then you can print them and investigate them, maybe even process them to aggregate the test statistics and p-values into a table? You can do that using a loop:
## object of save fitted objects in
obj <- vector(mode = "list", length = 2)
## loop
for(y in seq_along(obj))
obj[[y]] <- shapiro.test(DF[,y])
We can then look at the models using
> obj[[1]]
Shapiro-Wilk normality test
data: DF[, y]
W = 0.9891, p-value = 0.5895
for example, or using lapply
, which takes care of setting up the object we use to store the results for us:
> obj2 <- lapply(DF, shapiro.test)
> obj2[[1]]
Shapiro-Wilk normality test
data: X[[1L]]
W = 0.9891, p-value = 0.5895
Say now I wanted to extract the W
and p-value
data, we can process the object storing all the results to extract the bits we want, e.g.:
> tab <- t(sapply(obj2, function(x) c(x$statistic, x$p.value)))
> colnames(tab) <- c("W", "p.value")
> tab
W p.value
A 0.9890621 5.894563e-01
B 0.4589731 1.754559e-17
Or for those with a penchant for significance stars:
> tab2 <- lapply(obj2, function(x) c(W = unname(x$statistic),
+ `p.value` = x$p.value))
> tab2 <- data.frame(do.call(rbind, tab2))
> printCoefmat(tab2, has.Pvalue = TRUE)
W p.value
A 0.9891 0.5895
B 0.4590 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
This has got to be better than firing output to the screen that you then have to pour through?
Thanks a lot, Gavin. I did not know that "print" was turned off in loops and functions. The doc didn't say that. I was also unfamiliar with "y in seq_along(obj)". Didn't know you could do that! Your answer is very helpful.
– Sal Leggio
Jan 21 '11 at 3:36
I didn't know about 'printCoefmat'. Great!!
– Rodrigo
Mar 29 '16 at 18:12
add a comment |
In a loop, automatic printing is turned off, as it is inside a function. You need to explicitly print
something in both cases if you want to see the output. The [1] 9
things you are getting is because you are explicitly printing the values of y
.
Here is an example of how you might want to consider going about doing this.
> DF <- data.frame(A = rnorm(100), B = rlnorm(100))
> y <- 1
> shapiro.test(DF[,y])
Shapiro-Wilk normality test
data: DF[, y]
W = 0.9891, p-value = 0.5895
So we have automatic printing. In the loop we would have to do this:
for(y in 1:2)
print(shapiro.test(DF[,y]))
If you want to print more tests out, then just add them as extra lines in the loop:
for(y in 1:2)
writeLines(paste("Shapiro Wilks Test for column", y))
print(shapiro.test(DF[,y]))
writeLines(paste("Anderson Darling Test for column", y))
print(ad.test(DF[,y]))
But that isn't very appealing unless you like reading through reams of output. Instead, why not save the fitted test objects and then you can print them and investigate them, maybe even process them to aggregate the test statistics and p-values into a table? You can do that using a loop:
## object of save fitted objects in
obj <- vector(mode = "list", length = 2)
## loop
for(y in seq_along(obj))
obj[[y]] <- shapiro.test(DF[,y])
We can then look at the models using
> obj[[1]]
Shapiro-Wilk normality test
data: DF[, y]
W = 0.9891, p-value = 0.5895
for example, or using lapply
, which takes care of setting up the object we use to store the results for us:
> obj2 <- lapply(DF, shapiro.test)
> obj2[[1]]
Shapiro-Wilk normality test
data: X[[1L]]
W = 0.9891, p-value = 0.5895
Say now I wanted to extract the W
and p-value
data, we can process the object storing all the results to extract the bits we want, e.g.:
> tab <- t(sapply(obj2, function(x) c(x$statistic, x$p.value)))
> colnames(tab) <- c("W", "p.value")
> tab
W p.value
A 0.9890621 5.894563e-01
B 0.4589731 1.754559e-17
Or for those with a penchant for significance stars:
> tab2 <- lapply(obj2, function(x) c(W = unname(x$statistic),
+ `p.value` = x$p.value))
> tab2 <- data.frame(do.call(rbind, tab2))
> printCoefmat(tab2, has.Pvalue = TRUE)
W p.value
A 0.9891 0.5895
B 0.4590 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
This has got to be better than firing output to the screen that you then have to pour through?
In a loop, automatic printing is turned off, as it is inside a function. You need to explicitly print
something in both cases if you want to see the output. The [1] 9
things you are getting is because you are explicitly printing the values of y
.
Here is an example of how you might want to consider going about doing this.
> DF <- data.frame(A = rnorm(100), B = rlnorm(100))
> y <- 1
> shapiro.test(DF[,y])
Shapiro-Wilk normality test
data: DF[, y]
W = 0.9891, p-value = 0.5895
So we have automatic printing. In the loop we would have to do this:
for(y in 1:2)
print(shapiro.test(DF[,y]))
If you want to print more tests out, then just add them as extra lines in the loop:
for(y in 1:2)
writeLines(paste("Shapiro Wilks Test for column", y))
print(shapiro.test(DF[,y]))
writeLines(paste("Anderson Darling Test for column", y))
print(ad.test(DF[,y]))
But that isn't very appealing unless you like reading through reams of output. Instead, why not save the fitted test objects and then you can print them and investigate them, maybe even process them to aggregate the test statistics and p-values into a table? You can do that using a loop:
## object of save fitted objects in
obj <- vector(mode = "list", length = 2)
## loop
for(y in seq_along(obj))
obj[[y]] <- shapiro.test(DF[,y])
We can then look at the models using
> obj[[1]]
Shapiro-Wilk normality test
data: DF[, y]
W = 0.9891, p-value = 0.5895
for example, or using lapply
, which takes care of setting up the object we use to store the results for us:
> obj2 <- lapply(DF, shapiro.test)
> obj2[[1]]
Shapiro-Wilk normality test
data: X[[1L]]
W = 0.9891, p-value = 0.5895
Say now I wanted to extract the W
and p-value
data, we can process the object storing all the results to extract the bits we want, e.g.:
> tab <- t(sapply(obj2, function(x) c(x$statistic, x$p.value)))
> colnames(tab) <- c("W", "p.value")
> tab
W p.value
A 0.9890621 5.894563e-01
B 0.4589731 1.754559e-17
Or for those with a penchant for significance stars:
> tab2 <- lapply(obj2, function(x) c(W = unname(x$statistic),
+ `p.value` = x$p.value))
> tab2 <- data.frame(do.call(rbind, tab2))
> printCoefmat(tab2, has.Pvalue = TRUE)
W p.value
A 0.9891 0.5895
B 0.4590 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
This has got to be better than firing output to the screen that you then have to pour through?
edited Jan 17 '11 at 18:41
answered Jan 17 '11 at 18:15
Gavin SimpsonGavin Simpson
137k19314389
137k19314389
Thanks a lot, Gavin. I did not know that "print" was turned off in loops and functions. The doc didn't say that. I was also unfamiliar with "y in seq_along(obj)". Didn't know you could do that! Your answer is very helpful.
– Sal Leggio
Jan 21 '11 at 3:36
I didn't know about 'printCoefmat'. Great!!
– Rodrigo
Mar 29 '16 at 18:12
add a comment |
Thanks a lot, Gavin. I did not know that "print" was turned off in loops and functions. The doc didn't say that. I was also unfamiliar with "y in seq_along(obj)". Didn't know you could do that! Your answer is very helpful.
– Sal Leggio
Jan 21 '11 at 3:36
I didn't know about 'printCoefmat'. Great!!
– Rodrigo
Mar 29 '16 at 18:12
Thanks a lot, Gavin. I did not know that "print" was turned off in loops and functions. The doc didn't say that. I was also unfamiliar with "y in seq_along(obj)". Didn't know you could do that! Your answer is very helpful.
– Sal Leggio
Jan 21 '11 at 3:36
Thanks a lot, Gavin. I did not know that "print" was turned off in loops and functions. The doc didn't say that. I was also unfamiliar with "y in seq_along(obj)". Didn't know you could do that! Your answer is very helpful.
– Sal Leggio
Jan 21 '11 at 3:36
I didn't know about 'printCoefmat'. Great!!
– Rodrigo
Mar 29 '16 at 18:12
I didn't know about 'printCoefmat'. Great!!
– Rodrigo
Mar 29 '16 at 18:12
add a comment |
Not a new answer, but in addition to the above: "flush.console()" is necessary to force printing to take place DURING the loop rather than after. Only reason I use print() during a loop is to show progress, e.g., of reading many files.
for (i in 1:10)
print(i)
flush.console()
for(j in 1:100000)
k <- 0
Up vote for answering the question stated in the title. This should be edited thus the title of this questions does not match the question itself.
– aldux
Oct 25 '12 at 15:27
Up vote for flush.console()
– szeta
Jul 29 '15 at 10:07
add a comment |
Not a new answer, but in addition to the above: "flush.console()" is necessary to force printing to take place DURING the loop rather than after. Only reason I use print() during a loop is to show progress, e.g., of reading many files.
for (i in 1:10)
print(i)
flush.console()
for(j in 1:100000)
k <- 0
Up vote for answering the question stated in the title. This should be edited thus the title of this questions does not match the question itself.
– aldux
Oct 25 '12 at 15:27
Up vote for flush.console()
– szeta
Jul 29 '15 at 10:07
add a comment |
Not a new answer, but in addition to the above: "flush.console()" is necessary to force printing to take place DURING the loop rather than after. Only reason I use print() during a loop is to show progress, e.g., of reading many files.
for (i in 1:10)
print(i)
flush.console()
for(j in 1:100000)
k <- 0
Not a new answer, but in addition to the above: "flush.console()" is necessary to force printing to take place DURING the loop rather than after. Only reason I use print() during a loop is to show progress, e.g., of reading many files.
for (i in 1:10)
print(i)
flush.console()
for(j in 1:100000)
k <- 0
edited Jan 17 '11 at 21:50
answered Jan 17 '11 at 20:18
J. Win.J. Win.
4,74742748
4,74742748
Up vote for answering the question stated in the title. This should be edited thus the title of this questions does not match the question itself.
– aldux
Oct 25 '12 at 15:27
Up vote for flush.console()
– szeta
Jul 29 '15 at 10:07
add a comment |
Up vote for answering the question stated in the title. This should be edited thus the title of this questions does not match the question itself.
– aldux
Oct 25 '12 at 15:27
Up vote for flush.console()
– szeta
Jul 29 '15 at 10:07
Up vote for answering the question stated in the title. This should be edited thus the title of this questions does not match the question itself.
– aldux
Oct 25 '12 at 15:27
Up vote for answering the question stated in the title. This should be edited thus the title of this questions does not match the question itself.
– aldux
Oct 25 '12 at 15:27
Up vote for flush.console()
– szeta
Jul 29 '15 at 10:07
Up vote for flush.console()
– szeta
Jul 29 '15 at 10:07
add a comment |
Fantastic answer from Gavin Simpson. I took the last bit of magic and turned it into a function.
sw.df <- function ( data )
obj <- lapply(data, shapiro.test)
tab <- lapply(obj, function(x) c(W = unname(x$statistic), `p.value` = x$p.value))
tab <- data.frame(do.call(rbind, tab))
printCoefmat(tab, has.Pvalue = TRUE)
Then you can just call it with your data frame
sw.df ( df )
And if you want to try a transformation:
sw.df ( log(df) )
add a comment |
Fantastic answer from Gavin Simpson. I took the last bit of magic and turned it into a function.
sw.df <- function ( data )
obj <- lapply(data, shapiro.test)
tab <- lapply(obj, function(x) c(W = unname(x$statistic), `p.value` = x$p.value))
tab <- data.frame(do.call(rbind, tab))
printCoefmat(tab, has.Pvalue = TRUE)
Then you can just call it with your data frame
sw.df ( df )
And if you want to try a transformation:
sw.df ( log(df) )
add a comment |
Fantastic answer from Gavin Simpson. I took the last bit of magic and turned it into a function.
sw.df <- function ( data )
obj <- lapply(data, shapiro.test)
tab <- lapply(obj, function(x) c(W = unname(x$statistic), `p.value` = x$p.value))
tab <- data.frame(do.call(rbind, tab))
printCoefmat(tab, has.Pvalue = TRUE)
Then you can just call it with your data frame
sw.df ( df )
And if you want to try a transformation:
sw.df ( log(df) )
Fantastic answer from Gavin Simpson. I took the last bit of magic and turned it into a function.
sw.df <- function ( data )
obj <- lapply(data, shapiro.test)
tab <- lapply(obj, function(x) c(W = unname(x$statistic), `p.value` = x$p.value))
tab <- data.frame(do.call(rbind, tab))
printCoefmat(tab, has.Pvalue = TRUE)
Then you can just call it with your data frame
sw.df ( df )
And if you want to try a transformation:
sw.df ( log(df) )
answered Dec 6 '11 at 16:38
RolandoRolando
5111
5111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4716152%2fwhy-do-r-objects-not-print-in-a-function-or-a-for-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what is
i
for - did you meany <- 1
in the first line?– Gavin Simpson
Jan 17 '11 at 18:13