R中的代码清理

时间:2015-07-02 11:01:33

标签: r code-cleanup

我有这段代码需要一些精简:

SlopeSBS00<-lm(SBSNy$SBS00[c(1:10,17:41)] ~ Numbers[c(1:10,17:41)])$coeff[2]
SlopeSBS01<-lm(SBSNy$SBS01[c(1:10,17:41)] ~ Numbers[c(1:10,17:41)])$coeff[2]
SlopeSBS02<-lm(SBSNy$SBS02[c(1:10,17:41)] ~ Numbers[c(1:10,17:41)])$coeff[2]
SlopeSBS03<-lm(SBSNy$SBS03[c(1:10,17:41)] ~ Numbers[c(1:10,17:41)])$coeff[2]
...
SlopeSBS23<-lm(SBSNy$SBS23[c(1:10,17:41)] ~ Numbers[c(1:10,17:41)])$coeff[2]

这里SBSNy是变量SBS的变换版本,它是规范化的,而Numbers是1:41的数字向量。 所以这个代码基本上为每一行做的是对每个SBS00到SBS23进行SBSNy的线性回归,对于列1:10和17:41。 Coeff [2]只输出这里需要的斜率。

我忘记添加内容

SlopeSBS00到SlopeSBS23需要组合成一个data.frame: 这样的事情: SlopeSBS&lt; -data.frame(SlopeSBS00,SlopeSBS01,...,SlopeSBS23)

非常感谢任何形式的指导或帮助这段代码

数据示例

SBSNy包含一个包含41个观察值和25个变量的data.frame:

Numbers    SBS00    SBS01    SBS02    ...    SBS23
1          1.600    1.735    1.644    ...    1.328
2          1.486    1.692    1.522    ...    1.301
3          1.421    1.597    1.370    ...    1.321
...        ...      ...      ...      ...    ...
41         1.286    1.395    1.182    ...    1.206

关于制造数据的代码的示例,尽管此数据的范围为0-100:

df1 <- as.data.frame(matrix(sample(0:100,24*41,replace=TRUE),nrow=41, ncol=24))
Numbers <-c(1:41)
SBSNy<-data.frame(Numbers,df1)
names(SBSNy)<-c("Numbers",sprintf('SBS%02d',0:23))

1 个答案:

答案 0 :(得分:1)

假设&#34; SBS00&#34;是您的数据框中的secont列&#34; SBSNy&#34;:

library(XML)
doc <- htmlParse(html, asText = TRUE)
invisible(lapply(getNodeSet(doc, "//text()"), function(txt) xmlValue(txt) <- xmlValue(txt, trim = TRUE) ))

doc
# <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# <html>
# <head><title>DTC Descriptor</title></head>
# <body>
# <p>This diagnostic procedure supports the following DTC:</p><title>Conditions for Running the DTC</title>
# <p>This is good</p>
# </body>
# </html>

cat(gsub("(<[^>]+>)\\s*(.*?)\\s*(</[^>]+>)", "\\1\\2\\3", html))
# <title>DTC Descriptor</title>
# <p>This diagnostic procedure supports the following DTC:</p>
# <title>Conditions for Running the DTC</title>
# <p>This is good</p>