我经常从维基百科中提取表格.Excel的Web导入对维基百科无效,因为它将整个页面视为表格.在谷歌电子表格中,我可以输入:
=ImportHtml("http://en.wikipedia.org/wiki/Upper_Peninsula_of_Michigan","table",3)
Run Code Online (Sandbox Code Playgroud)
此功能将从该页面下载第3张表,其中列出了密歇根州UP的所有县.
R中有类似的东西吗?或者可以通过用户定义的函数创建?
这是我的问题的后续行动.我正在使用nltk解析人员,组织及其关系.通过这个例子,我能够创建大量的人员和组织; 但是,我在nltk.sem.extract_rel命令中收到错误:
AttributeError: 'Tree' object has no attribute 'text'
Run Code Online (Sandbox Code Playgroud)
这是完整的代码:
import nltk
import re
#billgatesbio from http://www.reuters.com/finance/stocks/officerProfile?symbol=MSFT.O&officerId=28066
with open('billgatesbio.txt', 'r') as f:
sample = f.read()
sentences = nltk.sent_tokenize(sample)
tokenized_sentences = [nltk.word_tokenize(sentence) for sentence in sentences]
tagged_sentences = [nltk.pos_tag(sentence) for sentence in tokenized_sentences]
chunked_sentences = nltk.batch_ne_chunk(tagged_sentences)
# tried plain ne_chunk instead of batch_ne_chunk as given in the book
#chunked_sentences = [nltk.ne_chunk(sentence) for sentence in tagged_sentences]
# pattern to find <person> served as <title> in <org> …Run Code Online (Sandbox Code Playgroud) 我正在为ggplot编写一个包装器,以根据各种数据集生成多个图形.当我将列名传递给函数时,我需要重命名列名,以便ggplot可以理解引用.
但是,我正在努力重命名数据框的列
这是一个数据框:
df <- data.frame(col1=1:3,col2=3:5,col3=6:8)
Run Code Online (Sandbox Code Playgroud)
这是我的搜索列名:
col1_search <- "col1"
col2_search <- "col2"
col3_search <- "col3"
Run Code Online (Sandbox Code Playgroud)
这里是要替换的列名:
col1_replace <- "new_col1"
col2_replace <- "new_col2"
col3_replace <- "new_col3"
Run Code Online (Sandbox Code Playgroud)
当我搜索列名时,R对列索引进行排序并忽略搜索位置.
例如,当我运行以下代码时,我希望新标题为new_col1,new_col2和new_col3,而新列名称为:new_col3,new_col2和new_col1
colnames(df)[names(df) %in% c(col3_search,col2_search,col1_search)] <- c(col3_replace,col2_replace,col1_replace)
Run Code Online (Sandbox Code Playgroud)
有没有人有解决方案,我可以搜索列名并按顺序替换它们?
在几天前将更新推送到OS X(El Capitan)之前,一切工作正常。其他命令工作正常,但是RStudio在绘制时开始冻结,因此我尝试了R。至少我看到带有R.app的旋转彩虹轮。
我从CRAN更新了R,然后从每晚的AT&T版本更新了R(由于R在Mac OS X Yosemite中冻结/挂起)。我更新/重新安装了XQuartz,但是在绘制时RStudio和R仍然没有响应。
我在RStudio日志文件中看不到任何内容
我还尝试删除.rhistory和其他可以找到的文件。
我尝试了基本r plot命令以及ggplot
我的R.version输出
$platform
[1] "x86_64-apple-darwin13.4.0"
$arch
[1] "x86_64"
$os
[1] "darwin13.4.0"
$system
[1] "x86_64, darwin13.4.0"
$status
[1] "RC"
$major
[1] "3"
$minor
[1] "3.0"
$year
[1] "2016"
$month
[1] "05"
$day
[1] "01"
$`svn rev`
[1] "70572"
$language
[1] "R"
$version.string
[1] "R version 3.3.0 RC (2016-05-01 r70572)"
$nickname
[1] "Supposedly Educational"
> sessionInfo()
R version 3.3.0 RC (2016-05-01 r70572)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running …Run Code Online (Sandbox Code Playgroud) 我一直在摸不着头脑.我有两个数据框:df
df <- data.frame(group = 1:3,
age = seq(30, 50, length.out = 3),
income = seq(100, 500, length.out = 3),
assets = seq(500, 800, length.out = 3))
Run Code Online (Sandbox Code Playgroud)
和 weights
weights <- data.frame(age = 5, income = 10)
Run Code Online (Sandbox Code Playgroud)
我想将这两个数据帧仅用于相同的列名称.我试过这样的事情:
colwise(function(x) {x * weights[names(x)]})(df)
Run Code Online (Sandbox Code Playgroud)
但这显然不起作用,因为colwise没有将列名保留在函数内.我查看了各种mapply解决方案(示例),但我无法得出答案.
结果data.frame应如下所示:
structure(list(group = 1:3, age = c(150, 200, 250), income = c(1000,
3000, 5000), assets = c(500, 650, 800)), .Names = c("group",
"age", "income", "assets"), row.names = c(NA, …Run Code Online (Sandbox Code Playgroud)