In the same ways that lines can be correctly indented using Ctrl + I or Cmd + I, is there a shortcut to automatically insert correct whitespace in RStudio scripts?
For example, for this:
df<-data.frame(x=c(1,2,3,4,5),y=c(3,4,5,6,7))
RStudio gives information saying "expected whitespace around '<-' operator" and "expected whitespace around '=' operator". Is there a shortcut to get this:
df <- data.frame(x = c(1, 2, 3, 4, 5), y = c(3, 4, 5, 6, 7))
Under RStudio you can select the code and type ctrl+shift+A for code reformatting, see RStudio shortcuts.
Result:
df <- data.frame(x = c(1, 2, 3, 4, 5), y = c(3, 4, 5, 6, 7))
Related
My homework asks me to create a sequence like this.
(1, 2, 2, 3, 4, 4, 5, 6, 6, . . . , 50, 50)
I'm a newbie to R so would really appreciate your help
You can use rep and alternate the number of repeats. Check ?rep for more information.
rep(1:50, ifelse(seq(1:50) %% 2, 1, 2))
Or, with two reps:
rep(1:50, rep(1:2, 25))
rep(1:50, rep(1:2, length.out = 50))
Short code using integer division:
2:76%/%1.5
This will return a numeric vector.
I have a question. I know it is not clear. However, I have 8 figures in 4 subplots. I want to border a line for each two of them and put the number around them. I attached a figure as follows. Could you please help me with that? I used the following simple example to plot this figures:
m1 <- matrix(c(
1, 2, 3, 4,
5, 6, 7, 8), nrow = 2, ncol = 4, byrow = TRUE)
layout(m1, widths = c(2.2, 1.5,2.2, 1.5))
par(mar=c(1,7,4,2))
I am trying to underline the mean value in the following plot:
dummy <- c(4, 9, 6, 5, 3)
barplot(dummy)
text(4, 8,paste('Average value', mean(dummy)))
I tried using underline() but it says it could not find the function.
text(4, 8,paste('Average value', underline(mean(dummy))))
Error:
could not find function "underline"
I am using : R version 3.1.0
Like this:
text(4, 8, bquote("Average value"~underline(.(mean(dummy)))))
or if you want the whole text underlined:
text(4, 8, bquote(underline("Average value"~.(mean(dummy)))))
Note use of bquote and .(x) to insert the value of a variable in the expression.
I could not access the link provided by #EddieSanders but I think this link is probably to the same solution: http://tolstoy.newcastle.edu.au/R/help/02a/0471.html
underlined <- function(x, y, label, ...){
text(x, y, label, ...)
sw <- strwidth(label)
sh <- strheight(label)
lines(x + c(-sw/2, sw/2), rep(y - 1.5*sh/2, 2))
}
dummy <- c(4, 9, 6, 5, 3)
barplot(dummy)
text(4, 8, underlined(4,8,paste('Average value', mean(dummy))), font=2)
EDIT:
This will underline just the mean value:
underlined <- function(x, y, label, ...){
text(x, y, label, ...)
sw <- strwidth(label)
sh <- strheight(label)
lines(x + c(-sw/2, sw/2), rep(y - 1.5*sh/2, 2))
}
dummy <- c(4, 9, 6, 5, 3)
barplot(dummy)
text(4, 8, paste('Average value', underlined(4.9,8,mean(dummy))))
I am trying to underline the mean value in the following plot:
dummy <- c(4, 9, 6, 5, 3)
barplot(dummy)
text(4, 8,paste('Average value', mean(dummy)))
I tried using underline() but it says it could not find the function.
text(4, 8,paste('Average value', underline(mean(dummy))))
Error:
could not find function "underline"
I am using : R version 3.1.0
Like this:
text(4, 8, bquote("Average value"~underline(.(mean(dummy)))))
or if you want the whole text underlined:
text(4, 8, bquote(underline("Average value"~.(mean(dummy)))))
Note use of bquote and .(x) to insert the value of a variable in the expression.
I could not access the link provided by #EddieSanders but I think this link is probably to the same solution: http://tolstoy.newcastle.edu.au/R/help/02a/0471.html
underlined <- function(x, y, label, ...){
text(x, y, label, ...)
sw <- strwidth(label)
sh <- strheight(label)
lines(x + c(-sw/2, sw/2), rep(y - 1.5*sh/2, 2))
}
dummy <- c(4, 9, 6, 5, 3)
barplot(dummy)
text(4, 8, underlined(4,8,paste('Average value', mean(dummy))), font=2)
EDIT:
This will underline just the mean value:
underlined <- function(x, y, label, ...){
text(x, y, label, ...)
sw <- strwidth(label)
sh <- strheight(label)
lines(x + c(-sw/2, sw/2), rep(y - 1.5*sh/2, 2))
}
dummy <- c(4, 9, 6, 5, 3)
barplot(dummy)
text(4, 8, paste('Average value', underlined(4.9,8,mean(dummy))))
I'm trying to export multiple panels of data and 1 plot as a single image using either the .png device or .pdf device and I'm not meeting with any success. I can produce the image that I want within R using the R native plotting device, but when I try to produce the same image directly, I get results that I do not anticipate.
Here is my example code
testMat <- matrix(1:20, ncol = 5)## create data
testMatDF <- as.data.frame(testMat)
names(testMatDF) <- c("Hey there", "Column 2",
"Some * Symbols", "And ^ More",
"Final Column")
rownames(testMatDF) <- paste("Group", 1:4)
library(gplots) ## gplots needed for textplot()
layout(matrix(c(1, 1, 2, 3, 3, 3), 2, 3, byrow = TRUE))
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
## produces what I want within R
layout(matrix(c(1, 1, 2, 3, 3, 3), 2, 3, byrow = TRUE))
png(file='plot1.png')
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
dev.off()
## only the last function texplot(testMatDF) gets output, not what I anticipated
I've also tried the mfrow() graphical parameter without success.
par(mfrow= c(3, 1))
png(file='plot2.png')
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
dev.off()
## only the last function texplot(testMatDF) gets output
If you move your calls to par or layout after you open your graphics device, it should work correctly.
png(file='plot2.png')
par(mfrow= c(3, 1))
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
dev.off()