我有一个以制表符分隔的文本文件,我导入到R.我使用以下命令进行导入:
data = read.table(soubor, header = TRUE, sep = "\t", dec = ".", colClasses =c("numeric","numeric","character","Date","numeric","numeric"))
Run Code Online (Sandbox Code Playgroud)
当我跑来str(data)检查我的列的数据类型时,我得到:
'data.frame': 211931 obs. of 6 variables:
$ DataValue : num 0 0 0 0 0 0 0 0 0 NA ...
$ SiteID : num 1 1 1 1 1 1 1 1 1 1 ...
$ VariableCode: chr "Sucho" "Sucho" "Sucho" "Sucho" ...
$ DateTimeUTC : Date, format: "2012-07-01" "2012-07-02" "2012-07-03" "2012-07-04" ...
$ Latitude : num 50.8 50.8 50.8 50.8 …Run Code Online (Sandbox Code Playgroud) 我是编写R包的新手.我正在努力学习如何为我的包装制作一个小插图.我创建了一个带有"getting-started.Rmd"文件的vignettes文件夹
---
title: "WaterML Tutorial"
author: "Jiri Kadlec"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Introduction to the WaterML R package}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
## Quick Start
This simple example shows how to get started with the <my R package>.
Run Code Online (Sandbox Code Playgroud)
要构建插图,我使用命令:
devtools::build_vignettes()
Run Code Online (Sandbox Code Playgroud)
然后我跑Rcmd.exe INSTALL my_package,并查看我的小插图我跑:
browseVignettes("my_package")
Run Code Online (Sandbox Code Playgroud)
但是我只看到html和源格式的小插图:

正如您在屏幕截图中看到的,没有"pdf"选项.如何配置我的.Rmd文件以pdf格式创建插图?
我在Windows 7 64位上使用R 3.2.0和RStudio 0.98.1103.我的电脑的Windows"区域和语言设置"是英语(美国).
出于某种原因,下面的代码在文本"Koryčanynadpřehradou"中用"c"和"r"替换了我的捷克字符"č"和"ř",当我从网上读取utf-8编码的XML文件时,解析XML文件到列表,并将列表转换为data.frame.
library(XML)
url <- "http://hydrodata.info/chmi-h/cuahsi_1_1.asmx/GetSiteInfoObject?site=CHMI-H:1263&authToken="
doc <- xmlRoot(xmlTreeParse(url, getDTD=FALSE, useInternalNodes = TRUE))
infoList <- xmlToList(doc[[2]][[1]])
siteName <- infoList$siteName
#this still displays correctly "Kory?any nad p?ehradou"
print(siteName)
#make a data.frame from the list item. I suspect here is the problem.
df <- data.frame(name=siteName, id=1)
#now the Czech characters are lost. I see only "Korycany nad prehradou"
View(df)
write.csv(df,"test.csv")
#the test.csv file also contains "Korycany nad prehradou"
#instead of "Kory?any nad p?ehradou"
Run Code Online (Sandbox Code Playgroud)
问题是什么?如何让R使用所有utf-8特殊字符正确显示我的data.frame并保存.csv文件而不会丢失"č"和"ř"捷克字符?
我正在尝试编写我的第一个R包.包中的函数取决于RCurl包中的getURL()函数.我按照以下教程:http: //r-pkgs.had.co.nz/和 http://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/
我安装了RTools,devtools和roxygen2来编写文档和构建软件包.
我的包名是"waterml".在我的包中,我有文件夹R,包含3个文件GetSites.R,GetVariables.R,GetValues.R.每个文件都有一个功能:
#' GetSites
#' @import XML
#' @importFrom RCurl getURL
#' This function gets the table of sites from the WaterML web service
#' @param server The URL of the web service ending with .asmx,
#' for example: http://worldwater.byu.edu/interactive/rushvalley/services/cuahsi_1_1.asmx
#' @keywords waterml
#' @export
#' @examples
#' GetSites("http://worldwater.byu.edu/interactive/rushvalley/services/cuahsi_1_1.asmx")
GetSites <- function(server) {
sites_url <- paste(server, "/GetSitesObject", sep="")
text <- RCurl::getURL(sites_url)
doc <- xmlRoot(xmlTreeParse(text, getDTD=FALSE, useInternalNodes = TRUE))
return(doc)
}
Run Code Online (Sandbox Code Playgroud)
现在,我尝试构建包:
library(devtools)
document()
Run Code Online (Sandbox Code Playgroud)
document()步骤完成且没有错误.现在我跑: …
我在共享主机上有一个MS SQL Server 2008数据库,我需要尽可能减少使用的存储空间.我最大的表有以下定义:
CREATE TABLE [stage](
[station_id] [smallint] NOT NULL,
[time_utc] [smalldatetime] NOT NULL,
[stage_mm] [smallint] NOT NULL,
CONSTRAINT [PK_stage] PRIMARY KEY CLUSTERED ([station_id] ASC,[time_utc] ASC)
Run Code Online (Sandbox Code Playgroud)
我试图找出表中每条记录的平均字节数.根据理论,大小应为:4B(行标题)+ 2B(smallint)+ 4B(smalldatetime)+ 2B(smallint),即12字节.
但是,当我运行命令时:
dbcc showcontig ('stage') with tableresults
Run Code Online (Sandbox Code Playgroud)
它显示:MinimumRecordSize = 15,MaximumRecordSize = 15因此根据SQL Server,每条记录的字节数为15而不是12当我查看表占用的总磁盘空间并将其除以时,每条记录的15字节数似乎也是正确的.行数.
占用3个额外字节的是什么?
我在R中有一个有两列的数据框,例如:
df = data.frame(x=c(1, 2, 3, 4), y=c(5, 6, 7, 8))
Run Code Online (Sandbox Code Playgroud)
我需要编写一个R脚本来将此data.frame转换为JSON数组数组,如下所示:
[[1, 5],[2, 6],[3, 7],[4, 8]]
Run Code Online (Sandbox Code Playgroud)
如何将我的数据帧转换为R中的JSON数组?
在 Google Earth Engine 中,我需要从ee.Date对象生成文件名。我在 Google Earth Engine 中有以下代码:
var date_object = ee.Date.fromYMD(2017,12, 1);
var date_string = date_object.format("YYYY-MM-dd");
print(date_string);
file_name = "my_file_" + date_string;
print(file_name);
Run Code Online (Sandbox Code Playgroud)
print(date_string)看起来不错的输出:
2017-12-01
Run Code Online (Sandbox Code Playgroud)
但是 print(file_name) 的输出是:
ee.String({
"type": "Invocation",
"arguments": {
"date": {
"type": "Invocation",
"arguments": {
"year": 2017,
"month": 12,
"day": 1
},
"functionName": "Date.fromYMD"
},
"format": "YYYY-MM-dd"
},
"functionName": "Date.format"
})
Run Code Online (Sandbox Code Playgroud)
我希望我会得到输出my_file_2017-12-01。如何ee.String在 Google EarthEngine 中使用带有对象的“+”运算符来连接两个字符串?
我需要在R中执行以下操作:例如N <- 7
,我有一个数字(N),我有一个长度(大小),例如size <- 3
例如,如果N == 7且size == 3,我需要得到一个输出相同的向量:
v <- c(1,1,1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3,3,3)
Run Code Online (Sandbox Code Playgroud)
你在v中看到,从1到大小的每个数字重复N次.有没有办法在没有使用for循环的情况下在R中执行此操作?
我在MySQL数据库中有下表:
CREATE TABLE `datavalues` (
`ValueID` int(11) NOT NULL AUTO_INCREMENT,
`DataValue` double NOT NULL,
`ValueAccuracy` double DEFAULT NULL,
`LocalDateTime` datetime NOT NULL,
`UTCOffset` double NOT NULL,
`DateTimeUTC` datetime NOT NULL,
`SiteID` int(11) NOT NULL,
`VariableID` int(11) NOT NULL,
`OffsetValue` double DEFAULT NULL,
`OffsetTypeID` int(11) DEFAULT NULL,
`CensorCode` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'nc',
`QualifierID` int(11) DEFAULT NULL,
`MethodID` int(11) NOT NULL DEFAULT '0',
`SourceID` int(11) NOT NULL,
`SampleID` int(11) DEFAULT NULL,
`DerivedFromID` int(11) DEFAULT NULL,
`QualityControlLevelID` int(11) NOT NULL …Run Code Online (Sandbox Code Playgroud) r ×6
dataframe ×2
date ×2
arrays ×1
database ×1
dependencies ×1
equality ×1
innodb ×1
javascript ×1
json ×1
knitr ×1
locale ×1
mysql ×1
package ×1
pdf ×1
phpmyadmin ×1
r-markdown ×1
rcurl ×1
roxygen2 ×1
sql-server ×1
sqldatatypes ×1
string ×1
utf-8 ×1
vignette ×1