在处理数据时(例如,在data.frame中),用户可以通过使用来控制显示数字
options(digits=3)
Run Code Online (Sandbox Code Playgroud)
并列出data.frame这样的.
ttf.all
Run Code Online (Sandbox Code Playgroud)
当用户需要像这样在Excell中粘贴数据时
write.table(ttf.all, 'clipboard', sep='\t',row.names=F)
Run Code Online (Sandbox Code Playgroud)
数字参数被忽略,数字不会舍入.
看到好的输出
> ttf.all
year V1.x.x V1.y.x ratio1 V1.x.y V1.y.y ratioR V1.x.x V1.y.x ratioAL V1.x.y V1.y.y ratioRL
1 2006 227 645 35.2 67 645 10.4 150 645 23.3 53 645 8.22
2 2007 639 1645 38.8 292 1645 17.8 384 1645 23.3 137 1645 8.33
3 2008 1531 3150 48.6 982 3150 31.2 755 3150 24.0 235 3150 7.46
4 2009 1625 3467 46.9 1026 3467 29.6 779 3467 22.5 222 3467 …
Run Code Online (Sandbox Code Playgroud) 有一个列表我想作为单个字符串输出到excel文件中.我从一个字符列表开始.
url="http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=21558518&retmode=xml"
xml = xmlTreeParse(url,useInternal = T)
ns <- getNodeSet(xml, '//PublicationTypeList/PublicationType')
types <- sapply(ns, function(x) { xmlValue(x) } )
types
Run Code Online (Sandbox Code Playgroud)
输出是这样的:
[1] "Journal Article" "Multicenter Study" "Research Support, N.I.H., Extramural"
[4] "Research Support, Non-U.S. Gov't"
Run Code Online (Sandbox Code Playgroud)
所以在类型中 - 有一个字符列表现在我需要制作一个字符串.这是我到目前为止,但它不是最佳的:
types_as_string = as.character(types[[1]])
if (length(types) > 1) for (j in 2:length(types)) types_as_string = paste(types_as_string,"| ",as.character(types[[j]]),sep="")
types_as_string
[1] "Journal Article| Multicenter Study| Research Support, N.I.H., Extramural| Research Support, Non-U.S. Gov't"
Run Code Online (Sandbox Code Playgroud)
所以我想最终得到一个由管道或其他分隔符分隔的漂亮字符串.(最后一个代码部分 - 我想要很好地重写).管道很重要,必须妥善完成.
导入文件后,我总是尝试从列名中删除空格,以便更容易地引用列名.
有没有更好的方法来执行此操作,然后使用transform然后删除此命令创建的额外列?
这就是我现在使用的:
names(ctm2)
#tranform function does this, but requires some action
ctm2<-transform(ctm2,dymmyvar=1)
#remove dummy column
ctm2$dymmyvar <- NULL
names(ctm2)
Run Code Online (Sandbox Code Playgroud) 这是一个简单的问题,但我无法弄清楚如何使用prop.table,我经常需要这个功能.
我有这样的数据
> library(ggplot2)
> #sample data(tips is a dataset within the ggplot2 package)
> head(tips,3)
total_bill tip sex smoker day time size
1 17 1.0 Female No Sun Dinner 2
2 10 1.7 Male No Sun Dinner 3
3 21 3.5 Male No Sun Dinner 3
> #how often there is a non-smoker
> table(tips$smoker)
No Yes
151 93
> #how many subjects
> nrow(tips)
[1] 244
Run Code Online (Sandbox Code Playgroud)
我需要知道吸烟者与非吸烟者的百分比这样的事情(丑陋的代码):
> #percentage of smokers
> options(digits=2)
> transform(as.data.frame(table(tips$smoker)),percentage_column=Freq/nrow(tips)*100)
Var1 Freq percentage_column …
Run Code Online (Sandbox Code Playgroud) 考虑具有混合数据类型的data.frame.
出于奇怪的目的,用户需要将所有列转换为字符.怎么做得最好?解决问题的整合尝试是这样的:
map(mtcars,as.character) %>% map_df(as.list) %>% View()
c2<-map(mtcars,as.character) %>% map_df(as.list)
Run Code Online (Sandbox Code Playgroud)
当我打电话时str(c2)
,应该说一个包含所有字符的tibble或data.frame.
另一种选择是对一些参数设置write.csv()
或write_csv()
实现生成的文件输出同样的事情.
从1600万个字符串变量的向量中删除德语(或法语)重音的最佳方法是什么.
例如,'Sjögren综合征'进入'干燥综合症'
将单个字符转换为单个字符比音译更好
ä=>aeö=>oeü=> ue.
例如,使用正则表达式将是一个选项,但有更好的东西(R包为此)?
gsub('ü','u',gsub('ö','o',"Sjögren's syndrome ( über) "))
对于非R平台有SO解决方案但对R来说不是很好的解决方案.
关于如何使用XML包中的readHTMLTable,我有很好的答案,我使用常规的http页面,但是我无法通过https页面解决我的问题.
我想在这个网站上阅读表格(网址字符串):
library(RTidyHTML)
library(XML)
url <- "https://ned.nih.gov/search/ViewDetails.aspx?NIHID=0010121048"
h = htmlParse(url)
tables <- readHTMLTable(url)
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个错误:文件https://ned.nih.gov/search/Vi...does不存在.
我试图通过https问题(下面的前两行)(使用谷歌找到解决方案)(例如:http://tonybreyal.wordpress.com/2012/01/13/ra-quick-scrape-of -top-grossing-films-from-boxofficemojo-com /).
这个技巧有助于查看更多页面,但任何提取表的尝试都无法正常工作.任何建议表示赞赏 我需要组织,组织标题,经理等表格字段.
#attempt to get past the https problem
raw <- getURL(url, followlocation = TRUE, cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
head(raw)
[1] "\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html;
...
h = htmlParse(raw)
Error in htmlParse(raw) : File ...
tables <- readHTMLTable(raw)
Error in htmlParse(doc) : File ...
Run Code Online (Sandbox Code Playgroud) 对于tidyverse用户,dplyr是处理数据的新方法.
对于试图避免使用旧包plyr的用户,dplyr中rbind.fill的等效功能是什么?
stringr包提供了良好的字符串函数.
搜索字符串(忽略大小写)
一个人可以用
stringr::str_detect('TOYOTA subaru',ignore.case('toyota'))
Run Code Online (Sandbox Code Playgroud)
这有效,但会发出警告
请使用(fixed | coll | regex)(x,ignore_case = TRUE)而不是ignore.case(x)
重写它的正确方法是什么?
编辑:%.%运算符现已弃用.使用magrittr中的%>%.
原始问题这个%.%
操作员做什么?我已经看到它在dplyr包中使用了很多,但似乎找不到任何有关它是什么或如何工作的支持文档.
它似乎将命令链接在一起,但据我所知......当我在它时,任何人都可以解释那些挂在%
标志上的特殊操作员的开局是什么以及何时在技术上是时候到了用它们代码更好?