我想编写一个函数来创建一组数字的绘图并添加一条回归线。截至目前,我仍然可以创建绘图,但我要么得到绘图错误,要么得到某种空响应。
我的函数没有不返回 null 的回归线,如下所示:
fun<-function(){
+ x<-c(1,2,3,4,5)
+ y<-c(1,2,3,4,5)
+ LR<-lm(x~y)
+
+ return(plot(x,y))
+ }
> fun()
Run Code Online (Sandbox Code Playgroud)
漂亮又漂亮,结果只是一个情节
如果我将回归线添加到图中,我仍然会得到该图,但我也会得到空响应
> fun<-function(){
+ x<-c(1,2,3,4,5)
+ y<-c(1,2,3,4,5)
+ LR<-lm(x~y)
+ p<-plot(x,y)
+ a<-abline(LR)
+ return(p)
+ }
> fun()
NULL
Run Code Online (Sandbox Code Playgroud)
或错误
> fun<-function(){
+ x<-c(1,2,3,4,5)
+ y<-c(1,2,3,4,5)
+
+ LR<-lm(x~y)
+
+ p<-plot(x,y)
+ a<-abline(LR)
+
+ return(p,a)
+ }
> fun()
Error in return(p, a) : multi-argument returns are not permitted
Run Code Online (Sandbox Code Playgroud)
或两个空值
> fun<-function(){
+ x<-c(1,2,3,4,5)
+ y<-c(1,2,3,4,5)
+
+ …Run Code Online (Sandbox Code Playgroud) 采取基本功能
fun<-function(){
x<-c(1,2,3,4,5)
y<-c(1,2,3,4,5)
t<-x+y
return(t)
}
Run Code Online (Sandbox Code Playgroud)
运行该函数后,有没有办法可以访问函数中创建的任何变量.通过指定变量 - 如下所示:
fun$y
Run Code Online (Sandbox Code Playgroud)
要么
fun$t
Run Code Online (Sandbox Code Playgroud)
或者是否有某种方法要求R将变量保存在函数中以便在我当前的R会话期间使用(我不打算永久保存它).AKA的一些东西:
fun<-function(){
x<-c(1,2,3,4,5)
y<-c(1,2,3,4,5)
t<-x+y
Y<-save y for latter use
T<-save T for latter use
return(t)
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我想写一个if语句,在满足某个条件之前会继续重复一个问题
像这样的东西:
fun<-function(){
x<-readline("what is x? ")
if(x>5)
{print("X must be less than 5")
**repeat lines 3 & 4**
}else{
print("Correct")}
}
Run Code Online (Sandbox Code Playgroud)
抱歉** - 但我不确定如何正确写出该行.我想要做的是每次输入大于5的数字时都要重复提示"什么是x",直到给出小于5的数字.理论上,功能看起来像这样
fun()
what is x? 6
X must be less than 5
what is x? 8
X must be less than 5
what is x? 3
Correct
Run Code Online (Sandbox Code Playgroud) 当使用ggplot在R中绘图时,我注意到有时如果你没有在y轴上指定任何限制,默认情况下,绘图在y轴的底部不会有任何"0"标记(假设是底角代表0).这个页面上的第一个图是一个很好的例子
http://wiki.stdout.org/rcookbook/Graphs/Axes%20%28ggplot2%29
您可以看到底角留空.这就是我想要的.
但是如果我指定y轴的极限,则始终显示0.所以如果我使用其中之一
scale_y_continuous(limits=c(0,8)
Run Code Online (Sandbox Code Playgroud)
要么
ylim(0,8)
Run Code Online (Sandbox Code Playgroud)
我得到那个小0和哈希标记
所以,如果我有一个数据集,如:
ByYear <- data.frame( V1 = c(2005,2006,2007,2008,2005,2006,2008,2006,2007,2005,2006,2007,2008),
+ V2 = c(0,0.2,0,1.6,2,5,0,4,3,0,8,0,5),
+ V3 = c('A','A','A','A','B','B','B','C','C','D','D','D','D'))
Run Code Online (Sandbox Code Playgroud)
并运行一个基本的情节
ggplot(data=ByYear,aes(x=V1,y=V2,group=V3))+geom_line()+geom_point(aes(shape=V3),size=3)+opts(panel.grid.major=theme_blank(),panel.grid.minor=theme_blank())
Run Code Online (Sandbox Code Playgroud)
有没有办法让第一个哈希标记为空白?类似的东西
ylim(,8) #(Even though it does not work)
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用
expand=c(0,0)) or yaxis="i"
Run Code Online (Sandbox Code Playgroud)
将0标记降低到下角(这是更好的),但唯一的问题是因为我有很多零数据这将切断点形状的底部所以我仍然需要下面的一点点缓冲空间零点.
Col1<-c(3,8,2,4,2)
Col2<-c(3,7,3,9,2)
Col3<-c(5,7,5,7,5)
Col4<-c(1,9,2,3,4)
Col5<-c(1,2,6,7,5)
Toy<-data.frame(Col1,Col2,Col3,Col4,Col5)
> Toy
Col1 Col2 Col3 Col4 Col5
1 3 3 5 1 1
2 8 7 7 9 2
3 2 3 5 2 6
4 4 9 7 3 7
5 2 2 5 4 5
Run Code Online (Sandbox Code Playgroud)
给定上面的数据框,我想删除每行中等于每行最大值的值,以及最大值的右边(或增加列#).
从逻辑上讲,该声明将是:
1. Find max value per row
2. Make max value for each row = NA
3. Make all columns to the right of column with max value =NA
Run Code Online (Sandbox Code Playgroud)
所以新的数据框架看起来像
Col1 Col2 Col3 Col4 Col5
1 …Run Code Online (Sandbox Code Playgroud) 我希望有一个我可以一次粘贴到R中的脚本.理想情况下,此脚本将包含散布各种函数的代码行.但是到目前为止,我必须一次一个地粘贴我的代码,因为(正如我已经写过的),当与下面的代码同时运行时,这些函数将无法工作.
对于下面的代码,我想让用户输入初始数据,然后对其进行计算.由于我不能连续运行几个"readline"调用,所以我将它们包装到一个名为Define的函数中.但是如果我立刻将它粘贴到R中,则以下代码将无效.
Define<-function(){
N<-readline("EnterDeployment # ")
Y<-as.numeric(readline("What Year was the Deployment? "))
L<-as.numeric(readline("What is the Distance? "))
return(list("Year"=Y,"Name"=N, "Length"=L))
}
Def<-Define()
Name<-Def$Name
Year<-Def$Year
L<-Def$Length
readline(paste("UPDN=",Name,"Year=",Year,"Distance=",L,"If this is correct press Return", sep=" "))
D<-L/4
F<-D*5
Run Code Online (Sandbox Code Playgroud)
但如果我把它分成几段,它就可以了.有没有办法将单个或多个函数合并到一行代码中?
我已经将四阶多项式曲线拟合到我的数据中,如下所示:
y<-c(-13,16,35,40,28,36,43,33,40,33,22,-5,-27,-31,-29,-25,-26,-31,-26,-24,-25,-29,-23,4)
x<-1:24
#4th order polynomial fit
fit<-lm(y~poly(x,4,raw=TRUE))
plot(x,y,ylim=c(min(y)-10,max(y)+10))
lines(x,predict(fit,data.frame(x=x)),col="red")
abline(h=0,lty=2)
Run Code Online (Sandbox Code Playgroud)
我的最终目标是计算这条曲线与零线相交的 3 个点。
所以首先,我需要延长曲线拟合的末端,使其第三次超过零线。完成此操作后,我想计算该方程通过零线的 3 个点。
r curve-fitting polynomial-math polynomials polynomial-approximations
我一直在努力学习在R中使用"if"和"for".作为一个简单的例子,我设置了数据框
V1<-c(3,2,2,4,5)
V2<-c(3,7,3,5,2)
V3<-c(5,2,5,7,5)
V4<-c(1,1,2,3,4)
V5<-c(1,2,6,7,5)
DF2<-data.frame(V1=V1,V2=V2,V3=V3,V4=V4,V5=V5)
DF2
V1 V2 V3 V4 V5
1 3 3 5 1 1
2 2 7 2 1 2
3 2 3 5 2 6
4 4 5 7 3 7
5 5 2 5 4 5
Run Code Online (Sandbox Code Playgroud)
我的目标是设置一个if语句,它将根据行值删除一行中的列.举个例子:
If V1 = 5, drop column V5
If V1 = 4, drop column V4 & V5
Run Code Online (Sandbox Code Playgroud)
所以根据这些规则,我的数据框最终会像这样
V1 V2 V3 V4 V5
1 3 3 5 1 1
2 2 7 2 1 2
3 2 …Run Code Online (Sandbox Code Playgroud) 给定一个简化的示例时间序列,按年份查看人口
Year<-c(2001,2002,2003,2004,2005,2006)
Pop<-c(1,4,7,9,20,21)
DF<-data.frame(Year,Pop)
Run Code Online (Sandbox Code Playgroud)
检验年份之间的变化/哪一年之间存在显着差异的最佳方法是什么?