我需要拆分单词和结束标记(某些类型的标点符号).奇怪的管道("|")可以算作结束标记.在我尝试添加管道之前,我的代码表示结束标记.添加管道使strsplit每个角色.逃避它导致和错误.如何在正则表达式中包含管道?
x <- "I like the dog|."
strsplit(x, "[[:space:]]|(?=[.!?*-])", perl=TRUE)
#[[1]]
#[1] "I" "like" "the" "dog|" "."
strsplit(x, "[[:space:]]|(?=[.!?*-\|])", perl=TRUE)
#Error: '\|' is an unrecognized escape in character string starting "[[:space:]]|(?=[.!?*-\|"
Run Code Online (Sandbox Code Playgroud)
结果我想:
#[[1]]
#[1] "I" "like" "the" "dog" "|" "." #pipe is an element
Run Code Online (Sandbox Code Playgroud) 我有一个关于R.的问题
我正在使用一个名为levene.test的测试来测试方差的同质性.
我知道你需要一个至少有两个级别的因子变量才能使它工作.从我看来,我确实至少有两个级别用于我正在使用的因子变量.但不知怎的,我不断得到错误:
> nocorlevene <- levene.test(geno1rs11809462$SIF1, geno1rs11809462$k, correction.method = "correction.factor")
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
Run Code Online (Sandbox Code Playgroud)
我甚至尝试从二项分布生成变量:
k<-rbinom(1304, 1, 0.5)
Run Code Online (Sandbox Code Playgroud)
然后使用它作为一个因素,但仍然无法正常工作.
最后,我创建了一个3级变量:
k<-sample(c(1,0,2), 1304, replace=T)
Run Code Online (Sandbox Code Playgroud)
但有些人仍然没有工作并得到同样的错误:
nocorlevene < - levene.test(geno1rs11809462 $ SIF1,geno1rs11809462 $ k,correction.method ="zero.removal")
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
Run Code Online (Sandbox Code Playgroud)
这是数据中变量类型的输出:
> str(geno1rs11809462)
'data.frame': 1304 obs. of 16 …Run Code Online (Sandbox Code Playgroud) 我试图最大化一个非常简单的可能性.在参数上未指定F的分布的意义上,这是非参数似然性.相反,对于每个观察到的xi,f(xi)=pi因此log(Likelihood)=Sum(log(f(xi)))=Sum(log(pi)).
我试图最大化的函数是:sum(log(pi))+lamda(sum(pi-1))
where sum(pi)=1(即这是一个约束最大化问题,可以使用拉格朗日乘数来解决).
这个问题的答案是数据点的数量pi=1/n在哪里n.但是,optimx似乎没有给出这个解决方案.有没有人有任何想法.如果n=2,我正在最大化的功能是log(p1)+log(p2)+lamda(p1+p2-1).
这是我的代码和R的输出:
n=2
log.like=function(p)
{
lamda=p[n+1]
ll=0
for(i in 1:n){
temp = log(p[i])+lamda*p[i]-lamda/(n)
ll=ll+temp
}
return(-ll)
}
mle = optimx(c(0.48,.52,-1.5),
log.like,
lower=c(rep(0.1,2),-3),
upper=c(rep(.9,2),-1),
method = "L-BFGS-B")
> mle
par fvalues method fns grs itns conv KKT1 KKT2 xtimes
1 0.9, 0.9, -1.0 1.010721 L-BFGS-B 8 8 NULL 0 FALSE NA 0
Run Code Online (Sandbox Code Playgroud)
等式的解决方案n=2是p1=p2=1/2和lamda=-2.但是,使用optimx时我没有得到这个.任何的想法?
我试图绘制两个变量:Eeff vs Neff使用格子包的xyplot函数.我想知道如何在这个散点图中添加一条线(这是普通R图形系统中的abline函数).
xyplot(Neff ~ Eeff, data = phuong,
xlab = "Energy efficiency (%)",
ylab = "Nitrogen efficiency (%)")
Run Code Online (Sandbox Code Playgroud) 我有一个"主"数据框,其中包含以下列:
userid, condition
Run Code Online (Sandbox Code Playgroud)
由于有四个实验条件,我还有四个带有答案信息的数据框,包括以下列:
userid, condition, answer1, answer2
Run Code Online (Sandbox Code Playgroud)
现在,我想加入这些,因此合并了用户ID,条件及其对这些条件的答案的所有组合.每个条件应该只在每行的相应列中具有正确的答案.
master = data.frame(userid=c("foo","foo","foo","foo","bar","bar","bar","bar"), condition=c("A","B","C","D","A","B","C","D"))
cond_a = data.frame(userid=c("foo","bar"), condition="A", answer1=c("1","1"), answer2=c("2","2"))
cond_b = data.frame(userid=c("foo","bar"), condition="B", answer1=c("3","3"), answer2=c("4","4"))
cond_c = data.frame(userid=c("foo","bar"), condition="C", answer1=c("5","5"), answer2=c("6","6"))
cond_d = data.frame(userid=c("foo","bar"), condition="D", answer1=c("7","7"), answer2=c("8","8"))
Run Code Online (Sandbox Code Playgroud)
如何将所有条件合并到主服务器中,因此主表如下所示?
userid condition answer1 answer2
1 bar A 1 2
2 bar B 3 4
3 bar C 5 6
4 bar D 7 8
5 foo A 1 2
6 foo B 3 4
7 foo C 5 6
8 …Run Code Online (Sandbox Code Playgroud) 我有一个有2列的数据框.
column1在column2中的随机数是一个地方保持列,我希望column3看起来像
random temp
0.502423373 1
0.687594055 0
0.741883739 0
0.445364032 0
0.50626137 0.5
0.516364981 0
...
Run Code Online (Sandbox Code Playgroud)
我想填充column3所以它取最后一个非零数字(在这个例子中是1或.5)并连续用该值填充以下行,直到它遇到一个具有不同数字的行.然后它重复整个列的过程.
random temp state
0.502423373 1 1
0.687594055 0 1
0.741883739 0 1
0.445364032 0 1
0.50626137 0.5 0.5
0.516364981 0 0.5
0.807804708 0 0.5
0.247948445 0 0.5
0.46573337 0 0.5
0.103705154 0 0.5
0.079625868 1 1
0.938928944 0 1
0.677713019 0 1
0.112231619 0 1
0.165907178 0 1
0.836195267 0 1
0.387712998 1 1
0.147737077 0 1
0.439281543 0.5 0.5
0.089013503 0 …Run Code Online (Sandbox Code Playgroud) 我有一个data.table包含多年变量的变量,即:
> dt <- data.table(id=1:3, A_2011=rnorm(3), A_2012=rnorm(3),
B_2011=rnorm(3), B_2012=rnorm(3),
C_2011=rnorm(3), C_2012=rnorm(3))
> dt
id A_2011 A_2012 B_2011 B_2012 C_2011 C_2012
1: 1 -0.8262134 0.832013744 -2.320136 0.1275409 -0.1344309 0.7360329
2: 2 0.9350433 0.279966534 -0.725613 0.2514631 1.0246772 -0.2009985
3: 3 1.1520396 -0.005775964 1.376447 -1.2826486 -0.8941282 0.7513872
Run Code Online (Sandbox Code Playgroud)
我希望按年将这个表融化为变量组,即:
> dtLong <- data.table(id=rep(dt[,id], 2), year=c(rep(2011, 3), rep(2012, 3)),
A=c(dt[,A_2011], dt[,A_2012]),
B=c(dt[,B_2011], dt[,B_2012]),
C=c(dt[,C_2011], dt[,C_2012]))
> dtLong
id year A B C
1: 1 2011 -0.826213405 -2.3201355 -0.1344309
2: 2 2011 0.935043336 -0.7256130 …Run Code Online (Sandbox Code Playgroud) 我在R中编写了一个函数,它将包含字母等级的数据框转换为数字等级.然后,我在数据框的每一列上使用sapply().有没有更简单的方法来执行此操作,不需要三次单独调用sapply?有没有办法将函数应用于数据框的每个元素而不是每个行或列?
源数据"成绩"如下所示:
grades <- read.table("Grades.txt", header = TRUE)
head(grades)
final_exam quiz_avg homework_avg
1 C A A
2 C- B- A
3 D+ B+ A
4 B+ B+ A
5 F B+ A
6 B A- A
Run Code Online (Sandbox Code Playgroud)
我的" convert_grades"函数如下所示:
convert_grades <- function(x) {
if (x == "A+") {
x <- 4.3
} else if (x == "A") {
x <- 4
} else if (x == "A-") {
x <- 3.7
} else if (x == "B+") {
x <- …Run Code Online (Sandbox Code Playgroud) 周末愉快.
我一直试图复制R中这篇博客文章的结果.我正在寻找一种不使用转换数据的方法t,最好使用tidyr或reshape.在下面的示例中,metadata通过转置获得data.
metadata <- data.frame(colnames(data), t(data[1:4, ]) )
colnames(metadata) <- t(metadata[1,])
metadata <- metadata[-1,]
metadata$Multiplier <- as.numeric(metadata$Multiplier)
Run Code Online (Sandbox Code Playgroud)
虽然它实现了我想要的东西,但我发现它并不那么不熟练.是否有任何有效的工作流程来转置数据框?
输入数据
data <- structure(list(Series.Description = c("Unit:", "Multiplier:",
"Currency:", "Unique Identifier: "), Nominal.Broad.Dollar.Index. = c("Index:_1997_Jan_100",
"1", NA, "H10/H10/JRXWTFB_N.M"), Nominal.Major.Currencies.Dollar.Index. = c("Index:_1973_Mar_100",
"1", NA, "H10/H10/JRXWTFN_N.M"), Nominal.Other.Important.Trading.Partners.Dollar.Index. = c("Index:_1997_Jan_100",
"1", NA, "H10/H10/JRXWTFO_N.M"), AUSTRALIA....SPOT.EXCHANGE.RATE..US..AUSTRALIAN...RECIPROCAL.OF.RXI_N.M.AL. = c("Currency:_Per_AUD",
"1", "USD", "H10/H10/RXI$US_N.M.AL"), SPOT.EXCHANGE.RATE...EURO.AREA. = c("Currency:_Per_EUR",
"1", "USD", "H10/H10/RXI$US_N.M.EU"), NEW.ZEALAND....SPOT.EXCHANGE.RATE..US..NZ...RECIPROCAL.OF.RXI_N.M.NZ.. = c("Currency:_Per_NZD",
"1", "USD", "H10/H10/RXI$US_N.M.NZ"), United.Kingdom....Spot.Exchange.Rate..US..Pound.Sterling.Reciprocal.of.rxi_n.m.uk = c("Currency:_Per_GBP", …Run Code Online (Sandbox Code Playgroud) 我一直在尝试使用检索网页(http://3sk.tv)的内容file_get_contents.不幸的是,结果输出缺少许多元素(图像,格式化,样式等等),而且基本上看起来与我试图检索的原始页面完全不同.
以前从未尝试过我尝试使用相同方法检索的任何其他URL,但由于某种原因,此特定URL(http://3sk.tv)拒绝正常工作.
我正在使用的代码是:
<?php
$homepage = file_get_contents('http://3sk.tv');
echo $homepage;
?>
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?关于如何正常工作的所有建议将不胜感激.谢谢大家的时间和考虑.
r ×9
dataframe ×2
reshape2 ×2
data.table ×1
join ×1
lattice ×1
maximization ×1
merge ×1
optimization ×1
php ×1
regex ×1
reshape ×1
tidyr ×1
variables ×1