小编Ben*_*ker的帖子

如何将字符串向量转换为Title Case

我有一个小写的字符串向量.我想将它们改为标题案例,这意味着每个单词的第一个字母都会被大写.我已经设法用一个双循环来做,但我希望有一个更有效和优雅的方式来做到这一点,也许是一个单行gsub和一个正则表达式.

这里有一些示例数据,以及有效的双循环,其次是我尝试过的其他不起作用的东西.

strings = c("first phrase", "another phrase to convert",
            "and here's another one", "last-one")

# For each string in the strings vector, find the position of each 
#  instance of a space followed by a letter
matches = gregexpr("\\b[a-z]+", strings) 

# For each string in the strings vector, convert the first letter 
#  of each word to upper case
for (i in 1:length(strings)) {

  # Extract the position of each regex match for the string in row …
Run Code Online (Sandbox Code Playgroud)

regex replace r

8
推荐指数
3
解决办法
6908
查看次数

补充DNA序列

假设我有一个DNA序列.我想得到它的补充.我使用了以下代码,但我没有得到它.我究竟做错了什么 ?

s=readline()
ATCTCGGCGCGCATCGCGTACGCTACTAGC
p=unlist(strsplit(s,""))
h=rep("N",nchar(s))
unlist(lapply(p,function(d){
for b in (1:nchar(s)) {    
    if (p[b]=="A") h[b]="T"
    if (p[b]=="T") h[b]="A"
    if (p[b]=="G") h[b]="C"
    if (p[b]=="C") h[b]="G"
}
Run Code Online (Sandbox Code Playgroud)

replace r bioinformatics complement genetics

8
推荐指数
5
解决办法
7028
查看次数

R - 强制某个参数在lm()中具有正系数

我想知道如何约束某些参数lm()以获得正系数.有一些包或函数(例如display)可以使所有系数和拦截为正.

例如,在这个例子中,我只想强迫x1并x2具有正系数.

    x1=c(NA,rnorm(99)*10)
    x2=c(NA,NA,rnorm(98)*10)
    x3=rnorm(100)*10
    y=sin(x1)+cos(x2)-x3+rnorm(100)

    lm(y~x1+x2+x3)

    Call:
      lm(formula = y ~ x1 + x2 + x3)       
    Coefficients:
      (Intercept)           x1           x2           x3  
    -0.06278      0.02261     -0.02233     -0.99626
Run Code Online (Sandbox Code Playgroud)

我试过功能nnnpls()包nnls,它可以轻松控制系数符号.遗憾的是,由于数据中的NA问题,我无法使用它,因为此函数不允许NA.

我看到函数NA可用于应用约束但我无法使其工作.

有人能让我知道我该怎么办?

regression r linear-regression lm glm

8
推荐指数
1
解决办法
6899
查看次数

为什么在这个例子中长度(f)和长度(g)之间存在差异?

f <- function() 1
g <- function() 2
class(g) <- "function"
class(f)  ## "function"
class(g)  ## "function"
length.function <- function(x) "function"
length(f)  ## 1
length(g)  ## "function"
Run Code Online (Sandbox Code Playgroud)

r

8
推荐指数
1
解决办法
123
查看次数

如何通过R中的线性插值来找到点

我有两个点(5,0.45)和(6,0.50)并且需要通过线性插值找到x = 5.019802时的值

但是如何在R中编码呢?

我得到了下面的代码,但只是得到一个图表insdeed.

x <- c(5,6)
y <- c(0.45,0.50)

interp <- approx(x,y)

plot(x,y,pch=16,cex=2)
points(interp,col='red')
Run Code Online (Sandbox Code Playgroud)

r linear-interpolation

8
推荐指数
1
解决办法
4913
查看次数

如何发布(sweave)回归公式?

如何很好地发布回归公式?

fit1<-dynlm(dep~indep1+indep2+indep3)
s1<-summary(fit1)
s1$call
Run Code Online (Sandbox Code Playgroud)

我怎么能发誓s1$call?我的意思是我不希望在我的pdf文档中有像dynlm(formula = dep~indep1 + indep2 + indep3)这样的东西.我希望在这个函数调用样式上有更多的教科书样式.另外我想(手动?)向模型添加拦截和错误(因为它实际上在那里).

请注意,我outreg在google上找到了(现在看起来有点太重了)并且一眼就看不到我的需求.

编辑:尝试发布示例输出,实际上我很喜欢,但我不知道如何使用SO编辑器做得更好:

 dep = alpha + beta_1*indep1 + beta_2*indep2 + beta_3*indep3 + epsilon
Run Code Online (Sandbox Code Playgroud)

一些矩阵表示法也可以,但打印模型定义无论如何都会很好.当然也可以手动添加它,但是当您处于稳健性检查阶段时,模型变量可能会经常更改,并且文档必须是最新的.

(使用http://texify.com :)

IMG] http://www.texify.com/img/%5CLARGE%5C%21%5Cmbox%7Bdep%7D%20%3D%20%5Calpha%20%2B%20%5Cbeta_1%20%5Ccdot%20%5Cmbox %7Bindep1%7D%20%2B%20%5Cbeta_2%20%5Ccdot%20%5Cmbox%7Bindep2%7D%20%2B%20%5Cepsilon.gif [/ IMG

publish regression r sweave

7
推荐指数
2
解决办法
1302
查看次数

如何防止R加载包?

我在R中使用多核包来并行化我的代码.但是,如果加载了tcltk软件包,使用多核软件包的分叉进程将导致R无限期挂起.所以我想阻止tcltk加载.如果任何包试图将其作为依赖项加载,我想要立即出错.这可能吗?

或者,我可以在加载包后卸载它吗?

r package

7
推荐指数
1
解决办法
923
查看次数

如何确定url对象是否返回'404 Not Found'?

简单地说:如果

x <- read.csv(url)
Run Code Online (Sandbox Code Playgroud)

存在,然后R将返回该URL的内容.一个很好的例子,如果你想尝试一下,可能是" http://ichart.finance.yahoo.com/table.csv?s=IBM&a=00&b=1&c=2008&d=03&e=4&f=2014&g=d&ignore=.csv ".该特定URL如果被分配给url并按上述方式运行,则会将包含过去5年IBM股票数据的Yahoo网站的data.frame加载到x中.

但是,如何预先知道,如果任何给定的网址会让你404?

就像是:

is.404.or.not(url)
Run Code Online (Sandbox Code Playgroud)

或者可能

status(connect.to(url))
Run Code Online (Sandbox Code Playgroud)

谢谢!

url r http-status-code-404

7
推荐指数
1
解决办法
3080
查看次数

从时间戳%j%Y%H:%M中删除前导零

我的时间戳在表格中

0992006 09:00
Run Code Online (Sandbox Code Playgroud)

我需要删除前导零以获取此表单:

992006 9:00
Run Code Online (Sandbox Code Playgroud)

这是我现在使用的代码,它不会删除前导零:

prediction$TIMESTAMP <- as.character(format(prediction$TIMESTAMP, '%j%Y %H:%M'))
Run Code Online (Sandbox Code Playgroud)

regex r date

7
推荐指数
3
解决办法
706
查看次数

轮廓()中没有指定正确的“z”矩阵

我正在尝试在 R 中绘制一个简单的等高线图,但出现以下错误"no proper 'z' matrix specified":以下是我的代码:

r=read.table("test.dat")
dim(r)
## [1] 56  3

x=matrix(unlist(r[,3]),ncol=112,byrow=T)
dim(x)
## [1] 1 11

image((1:nrow(x))-56,(1:ncol(x))-56,x)
contour((1:nrow(x))-56,(1:ncol(x))-56,x , add=TRUE)

## Error in contour.default((1:nrow(x)) - 56,
##   (1:ncol(x)) - 56, x, add = TRUE) :
##     no proper 'z' matrix specified
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决我犯错误的地方吗?

r contour

7
推荐指数
1
解决办法
5254
查看次数