更改 data.frame 值

发布于 2024-10-09 15:50:03 字数 836 浏览 0 评论 0原文

背景

区分模型值和预测值。

问题

考虑以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2024-10-16 15:50:03

创建数据框时,将模型的变量类型设置为字符而不是默认值(因子)。这可以在创建数据框时完成。

df <- data.frame( value=p, model='y',  stringsAsFactors=FALSE)

然后,您可以将任何字符值分配给数据框中的模型变量。

R> df[201:210,2] <- 'n'
R> table(df[,2])
  n   y 
 10 200 

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.

df <- data.frame( value=p, model='y',  stringsAsFactors=FALSE)

Then you can assign any character value to the model variable in the data frame.

R> df[201:210,2] <- 'n'
R> table(df[,2])
  n   y 
 10 200 
笨死的猪 2024-10-16 15:50:03

该列是一个因素:

R> sapply(df, class)
    value     model 
"numeric"  "factor" 
R> 

并且只有一个级别:

R> table(df[,2])

  y 
200 

您可能需要重新级别以允许“n”。

编辑:现在重新审视这一点。您的 gam() 模型不使用第二列,所以简单地这样做有什么问题

R> predict(gam.object, data.frame(x=201:210))
    1     2     3     4     5     6     7     8     9    10 
1.370 1.379 1.388 1.397 1.406 1.415 1.424 1.433 1.442 1.450 
R> 

换句话说,您既不需要 y 也不需要 n 但也许我在这里误解了一些东西。如果是这样,您能否修改您的问题并使其更清楚?

The column is a factor:

R> sapply(df, class)
    value     model 
"numeric"  "factor" 
R> 

and has only one level:

R> table(df[,2])

  y 
200 

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 doing

R> predict(gam.object, data.frame(x=201:210))
    1     2     3     4     5     6     7     8     9    10 
1.370 1.379 1.388 1.397 1.406 1.415 1.424 1.433 1.442 1.450 
R> 

In other words, you need neither the y nor the n but maybe I am misunderstanding something here. If so, could you please amend your question and make it clearer?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文