我试图通过使用内置方法在文本中找到与NLTK的搭配.
现在我有以下示例文本(test和foo相互跟随,但两者之间有一个句子边框):
content_part = """test. foo 0 test. foo 1 test.
foo 2 test. foo 3 test. foo 4 test. foo 5"""
Run Code Online (Sandbox Code Playgroud)
标记化的结果collocations()
如下:
print nltk.word_tokenize(content_part)
# ['test.', 'foo', 'my', 'test.', 'foo', '1', 'test.',
# 'foo', '2', 'test.', 'foo', '3', 'test.', 'foo', '4', 'test.', 'foo', '5']
print nltk.Text(nltk.word_tokenize(content_part)).collocations()
# test. foo
Run Code Online (Sandbox Code Playgroud)
如何防止NLTK:
所以在这个例子中它根本不应该打印任何搭配,但我想你可以设想更复杂的文本,其中句子中也有搭配.
我可以猜测我需要使用Punkt句子分段器,但后来我不知道如何将它们再次组合起来找到与nltk的搭配(collocation()
似乎比仅仅计算东西更强大).
一段时间以来我一直在使用ff包来处理大数据.我使用的R对象有大约130,000,000行和14列.其中两个列,温度和降水值缺少值"NA",因此我需要删除这些行以继续我的工作.我一直试图像在普通的R对象中那样做:
data<-data[!is.na(data$temp),]
Run Code Online (Sandbox Code Playgroud)
但我一直收到一个错误:
Error: vmode(index) == "integer" is not TRUE
Run Code Online (Sandbox Code Playgroud)
有没有人能够删除ffdf对象中的行?我很感激任何帮助.
我已经下载了多个.txt.gz文件用于Hadley海表温度观测。数据已解压缩,从而产生了多个ASCII格式的 .txt文件。
我有以下文件(R脚本是我正在处理的文件):
list.files()
[1] "Get_SST_Data.R" "HadISST1_SST_1931-1960.txt" "HadISST1_SST_1931-1960.txt.gz"
[4] "HadISST1_SST_1961-1990.txt" "HadISST1_SST_1961-1990.txt.gz" "HadISST1_SST_1991-2003.txt"
[7] "HadISST1_SST_2004.txt" "HadISST1_SST_2005.txt" "HadISST1_SST_2006.txt"
[10] "HadISST1_SST_2007.txt" "HadISST1_SST_2008.txt" "HadISST1_SST_2009.txt"
[13] "HadISST1_SST_2010.txt" "HadISST1_SST_2011.txt" "HadISST1_SST_2012.txt"
[16] "HadISST1_SST_2013.txt"
Run Code Online (Sandbox Code Playgroud)
我希望能够利用温度数据为1950年以来每天的海面温度创建一个数值矢量,从而最终绘制一个时间序列图。
看起来像这样
[ps这仅供参考...]
提前致谢!
我最近遇到了R代码,它将POSIXlt日期对象与日期字符串进行了比较.
as.POSIXlt.date("2007-02-02") >= "2007-02-01"
[1] FALSE
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,结果至少对我来说是假的.我期待POSIXlt对象被强制转换为字符向量,因此不等式应该测试为TRUE.然后我尝试了明确的强制,并强迫任何一方进入另一方的类型,产生了真实.
as.character(as.POSIXlt.date("2007-02-02")) >= "2007-02-01"
[1] TRUE
Run Code Online (Sandbox Code Playgroud)
和
as.POSIXlt.date("2007-02-02") >= as.POSIXlt.date("2007-02-01")
[1] TRUE
Run Code Online (Sandbox Code Playgroud)
我认为将LHS日期对象强制转换为字符向量在语义上是错误的,因为那时的比较将是词典,而不是预期的(尽管在这种情况下它的计算结果为TRUE).我对吗?
在我看来,第三个表达是语义上正确的代码.但为什么第一个代码不起作用(评估为FALSE)?在比较它们之前,R是否强制转换为字符向量?
这是我的平台信息:
R version 3.1.0 (2014-04-10) -- "Spring Dance"
Platform: x86_64-redhat-linux-gnu (64-bit)
Run Code Online (Sandbox Code Playgroud)
我是R的新手.非常感谢任何帮助.
谢谢,法尔汉
我有一个postgres数据库,其中包含用户表(userid,firstname,lastname)和usermetadata表(userid,代码,内容,创建的日期时间).我按代码在usermetadata表中存储有关每个用户的各种信息,并保留完整的历史记录.例如,用户(用户ID 15)具有以下元数据:
15, 'QHS', '20', '2008-08-24 13:36:33.465567-04'
15, 'QHE', '8', '2008-08-24 12:07:08.660519-04'
15, 'QHS', '21', '2008-08-24 09:44:44.39354-04'
15, 'QHE', '10', '2008-08-24 08:47:57.672058-04'
Run Code Online (Sandbox Code Playgroud)
我需要获取所有用户的列表以及各种用户元数据代码的最新值.我以编程方式做到了这一点,当然是神圣的缓慢.在SQL中我能想到的最好的方法是加入子选择,这也很慢,我必须为每个代码做一个.
I am trying to create a tesselation of filled hexagons (polygons centered around a hexagonally-spaced lattice) in ggplot2. I have accomplished this using the 'plot' command but am struggling transitioning this to ggplot.
Here is the code for the set-up:
# Generate a lattice of points equally spaced in the centers of a hexagonal lattice
dist = 1 # distance between the centers of hexagons
nx = dist*15 # horizontal extent
ny = dist*15 # vertical extent
MakeHexLattice = function(nx, …
Run Code Online (Sandbox Code Playgroud) 我有成对的关系,就像这样
col_combi = [('a','b'), ('b','c'), ('d','e'), ('l','j'), ('c','g'),
('e','m'), ('m','z'), ('z','p'), ('t','k'), ('k', 'n'),
('j','k')]
Run Code Online (Sandbox Code Playgroud)
这种关系的数量足够大,可以单独检查.这些元组表示两个值都相同.我想申请传递性并找出共同的群体.输出如下:
[('a','b','c','g'), ('d','e','m','z','p'), ('t','k','n','l','j')]
Run Code Online (Sandbox Code Playgroud)
我尝试了下面的代码,但它有bug,
common_cols = []
common_group_count = 0
for (c1, c2) in col_combi:
found = False
for i in range(len(common_cols)):
if (c1 in common_cols[i]):
common_cols[i].append(c2)
found = True
break
elif (c2 in common_cols[i]):
common_cols[i].append(c1)
found = True
break
if not found:
common_cols.append([c1,c2])
Run Code Online (Sandbox Code Playgroud)
以上代码的输出如下
[['a', 'b', 'c', 'g'], ['d', 'e', 'm', 'z', 'p'], ['l', 'j', 'k'], ['t', 'k', 'n']]
Run Code Online (Sandbox Code Playgroud)
我知道为什么这段代码不起作用.所以我想知道如何执行此任务.
提前致谢
n <- akima :: interp(
x, y, z, xo = seq(min(x), max(x), length = 100),
yo = seq(min(y), max(y), length = 100),
linear = TRUE, extrap = FALSE,
duplicate = "error", dupfun = NULL,
ncp = NULL)
Run Code Online (Sandbox Code Playgroud)
但它给出了:
Error in interp.old(x, y, z, xo = xo, yo = yo, ncp = 0, extrap = extrap, :
missing values and Infs not allowed
Run Code Online (Sandbox Code Playgroud)
我们来看看:
is.na(x), is.na(y), is.na(z)
都是假的
str(x)
num [1:1398] 1.62 1.62 1.62 1.62 1.63 1.63 1.63 1.63 1.63 1.63 …
Run Code Online (Sandbox Code Playgroud) 我有一个表格的数据框:
LociDT4Length
[[1]]
Cohort V1
1: CEU 237
2: Lupus 203
3: RA 298
4: YRI 278
[[2]]
Cohort V1
1: CEU 625
2: Lupus 569
3: RA 1022
4: YRI 762
[[3]]
Cohort V1
1: CEU 161
2: Lupus 203
3: RA 268
4: YRI 285
[[4]]
Cohort V1
1: CEU 1631
2: Lupus 1363
3: RA 1705
4: YRI 1887
Run Code Online (Sandbox Code Playgroud)
几天前,我学会了这个命令:
with(LociDT4Length[[1]], ifelse(Cohort=="RA", V1/62,
ifelse(Cohort=="Lupus", V1/62,
ifelse(Cohort=="CEU", V1/96,
ifelse(Cohort=="YRI", V1/80,NA)))))
Run Code Online (Sandbox Code Playgroud)
适当地返回结果:
[1] 2.468750 3.274194 4.806452 3.475000
Run Code Online (Sandbox Code Playgroud)
但是,我尝试将此语句置于循环中会为每个嵌套的DF返回一个警告,并返回不正确的结果.错误消息是:
1: …
Run Code Online (Sandbox Code Playgroud) 我收到此错误:
Error in x$getinverse : $ operator is invalid for atomic vectors
Run Code Online (Sandbox Code Playgroud)
我的代码是这个。我不明白我在哪里犯错。
##create a function which starts with a null matrix argument
makeCacheMatrix <- function(x = matrix()) {
## initialize the value of the matrix inverse to NULL
matrixinverse <- NULL
## delcare another function set where the value will be cached in 1. Matrix is created
## for the first time. 2. changes made to cached matrix
set <- function(y) {
x <<- y
## change the value …
Run Code Online (Sandbox Code Playgroud) 作为练习,我进行了以下测试:
require "silly_blocks"
describe "some silly block functions" do
describe "reverser" do
it "reverses the string returned by the default block" do
result = reverser do
"hello"
end
result.should == "olleh"
end
it "reverses each word in the string returned by the default block" do
result = reverser do
"hello dolly"
end
result.should == "olleh yllod"
end
end
describe "adder" do
it "adds one to the value returned by the default block" do
adder do
5
end.should == 6
end …
Run Code Online (Sandbox Code Playgroud)