更改 data.frame 值
背景
区分模型值和预测值。
问题
考虑以下代码:
library( 'gam' )
slope = 0.55
amplitude = 0.22
frequency = 3
noise = 0.75
x <- 1:200
y <- (slope * x / 100) + (amplitude * sin( frequency * x / 100 ))
ynoise <- y + (noise * runif( length( x ) ))
gam.object <- gam( ynoise ~ s( x ) )
p <- predict( gam.object, data.frame( x = 1:210 ) )
df <- data.frame( value=p, model='y' )
问题
将数据框 (df
) 的某些 model
行设置为 'n'
的 R 语法是什么? :
df[201:210,2] <- 'n'
不起作用,我尝试过的任何变体也不起作用。
相关
http://stat. ethz.ch/R-manual/R-patched/library/base/html/Extract.data.frame.html
谢谢!
Background
Distinguish between model values and predicted values.
Problem
Consider the following code:
library( 'gam' )
slope = 0.55
amplitude = 0.22
frequency = 3
noise = 0.75
x <- 1:200
y <- (slope * x / 100) + (amplitude * sin( frequency * x / 100 ))
ynoise <- y + (noise * runif( length( x ) ))
gam.object <- gam( ynoise ~ s( x ) )
p <- predict( gam.object, data.frame( x = 1:210 ) )
df <- data.frame( value=p, model='y' )
Question
What is the R syntax to set some model
rows of the data frame (df
) to 'n'
?:
df[201:210,2] <- 'n'
Doesn't work, nor do any of the variations I have tried.
Related
http://stat.ethz.ch/R-manual/R-patched/library/base/html/Extract.data.frame.html
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建数据框时,将模型的变量类型设置为字符而不是默认值(因子)。这可以在创建数据框时完成。
然后,您可以将任何字符值分配给数据框中的模型变量。
When you create the data frame, set the type of variable for the model to character rather than the default, which is factor. This can be done when you make the data frame.
Then you can assign any character value to the model variable in the data frame.
该列是一个因素:
并且只有一个级别:
您可能需要重新级别以允许“n”。
编辑:现在重新审视这一点。您的 gam() 模型不使用第二列,所以简单地这样做有什么问题
换句话说,您既不需要
y
也不需要n 但也许我在这里误解了一些东西。如果是这样,您能否修改您的问题并使其更清楚?
The column is a factor:
and has only one level:
You probably need to re-level this to allow for 'n'.
Edit: Revisiting this now. Your
gam()
model does not use this second column, so what is wrong with simply doingIn other words, you need neither the
y
nor then
but maybe I am misunderstanding something here. If so, could you please amend your question and make it clearer?