我有一个函数fetchWeather(city,month1,year1,...),可以无限组合月/年输入并输出这些时期的天气数据。
我想通过速记进行一些组合,使其更加紧凑。例如,而不是
fetchWeather(Boston,4,2015,5,2015,6,2015,7,2015,8,2015,9,2015,10,2015)
Run Code Online (Sandbox Code Playgroud)
我想做
jv15<- c(4,2015,5,2015,6,2015,7,2015,8,2015,9,2015,10,2015)
Run Code Online (Sandbox Code Playgroud)
然后打电话
fetchWeather(Boston,jv15)
Run Code Online (Sandbox Code Playgroud)
但我无法让它发挥作用。
我对 R 很陌生,我想这是一个非常简单的解决方案,但我似乎无法弄清楚。任何帮助表示赞赏。
我正在学习 R 编程,因此遇到了一些问题 - 在您的帮助下已经能够解决这些问题。
但我现在需要重命名数据框的列。我有一个带有 2 列的翻译数据框,其中包含列名称和新列的名称。
这是我的代码:我的问题是如何从trans数据框中选择两列并将它们用作trans$old和trans$new变量?
我有 7 列要重命名,因此翻译表可能会更长。
replace_header <- function()
{
names(industries)[names(industries)==trans$old] <- trans$new
replaced <- industries
return (replaced)
}
replaced_industries <- replace_header()
Run Code Online (Sandbox Code Playgroud) 我想创建一个矩阵,其中包含从一个数据帧到另一个数据帧的行的欧几里德距离.例如,假设我有以下数据框:
a <- c(1,2,3,4,5)
b <- c(5,4,3,2,1)
c <- c(5,4,1,2,3)
df1 <- data.frame(a,b,c)
a2 <- c(2,7,1,2,3)
b2 <- c(7,6,5,4,3)
c2 <- c(1,2,3,4,5)
df2 <- data.frame(a2,b2,c2)
Run Code Online (Sandbox Code Playgroud)
我想创建一个矩阵,其中df1中每行的距离与df2的行相距.
因此矩阵[2,1]应该是df1 [2,]和df2 [1,]之间的欧氏距离.矩阵[3,2] df [3,]和df2 [2,]等之间的距离.
有谁知道如何实现这一目标?
我有两个数据框:
as1 <- data.frame(ID = c(1,2,3,4,5,6),
pID = c(21,22,23,24,25,26),
Values = c(435,33,45,NA, NA,12))
as2 <- data.frame(ID = c(4,5),
pid = c(24,25),
Values = c(544, 676))
Run Code Online (Sandbox Code Playgroud)
我需要通过匹配ID和pID将as1中的NA值替换为as2中的NA值
我需要获取结果数据框架为:
resultdf
ID pID Values
1 1 21 435
2 2 22 33
3 3 23 45
4 4 24 544
5 5 25 676
6 6 26 12
Run Code Online (Sandbox Code Playgroud)
我尝试先做子集,然后na.omit()再执行rbind...但是我丢失了索引。
我有一个简单的R代码,我正在从文件中读取文本并在条形图上绘制重复的短语.出于某种原因,条形图仅显示单个单词而不是多个措辞短语.我哪里错了?
install.packages("xlsx")
install.packages("tm")
install.packages("wordcloud")
install.packages("ggplot2")
library(xlsx)
library(tm)
library(wordcloud)
library(ggplot2)
setwd("C://Users//608447283//desktop//R_word_charts")
test <- Corpus(DirSource"C://Users//608447283//desktop//R_word_charts//source"))
test <- tm_map(test, stripWhitespace)
test <- tm_map(test, tolower)
test <- tm_map(test, removeWords,stopwords("english"))
test <- tm_map(test, removePunctuation)
test <- tm_map(test, PlainTextDocument)
tok <- function(x) NGramTokenizer(x, Weka_control(min=3, max=10))
tdm <- TermDocumentMatrix(test,control = list(tokenize = tok))
termFreq <- rowSums(as.matrix(tdm))
termFreq <- subset(termFreq, termFreq>=50)
write.csv(termFreq,file="TestCSV1")
TestCSV <- read.csv("C:/Users/608447283/Desktop/R_word_charts/TestCSV1")
ggplot(data=TestCSV, aes(x=X, y=x)) +
geom_bar(stat="identity") + theme(axis.text.x = element_text(angle = 90, hjust = 1))
Run Code Online (Sandbox Code Playgroud)
我的输出:
样本数据: 样本数据提取
列表:
terms <- list(Item1 = c("a", "b", "c", "d"),
Item2 = c("a", "e", "f", "g"),
Item3 = c("b", "e", "h", "i"),
Item4 = c("j", "k"))
Run Code Online (Sandbox Code Playgroud)
我想获得列表中每对项目之间共享字母的数量.因此预期的输出是:
[,1] [,2] [,3] [,4]
[1,] 4 1 1 0
[2,] 1 4 1 0
[3,] 1 1 4 0
[4,] 0 0 0 2
Run Code Online (Sandbox Code Playgroud)
从之前的StackOverflow答案中,我找到了一个可能的解决方案:
overlapLength <- function(x, y) mapply(function(x, y)
length(intersect(x, y)), terms[x], terms[y])
s <- seq_along(terms)
outer(s, s, overlapLength)
Run Code Online (Sandbox Code Playgroud)
但这对我的名单来说非常慢,这个名单非常大(约9,000件).
有更快的方法吗?
谢谢各位的意见.我用我列表中的前100个项目计算了所有答案.
> system.time(f_crossprod(go))
user system elapsed
0.024 0.001 0.025
> system.time(f_crossprod2(go)) …Run Code Online (Sandbox Code Playgroud) R不会自动绘制英文轴标签(例如,月份是法语).我使用(如果信息有用......):
我知道我不是唯一有这个问题的人,但我从来没有找到解决方案.
> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252 LC_NUMERIC=C LC_TIME=French_France.1252
Run Code Online (Sandbox Code Playgroud) 有谁知道如何在ggplot上使用操作(),以便轻松选择平滑(跨度)级别?我尝试了以下但没有成功:
# fake data
xvals <- 1:10
yvals <- xvals^2*exp(rnorm(2,5,0.6))
data <- data.frame(xvals,yvals)
# plot with manipulate
manipulate(
ggplot(data,aes(xvals,yvals)) +
geom_smooth(span=slider(0.5,5)) +
geom_point()
)
Run Code Online (Sandbox Code Playgroud)
我希望能够轻松地通过“平滑级别”。
我在ubuntu精确穿山甲在VM内运行,使用R 3.1和Rstudio 0.98.507并且我无法安装ggplot2
这是输出:
install.packages('ggplot2')
Installing package into ‘/home/uwhpsc/R/i686-pc-linux-gnu-library/3.1’
(as ‘lib’ is unspecified)
also installing the dependencies ‘colorspace’, ‘Rcpp’, ‘stringr’, ‘RColorBrewer’, ‘dichromat’, ‘munsell’, ‘labeling’, ‘plyr’, ‘digest’, ‘gtable’, ‘reshape2’, ‘scales’, ‘proto’
trying URL 'http://cran.rstudio.com/src/contrib/colorspace_1.2-4.tar.gz'
Content type 'application/x-gzip' length 242791 bytes (237 Kb)
opened URL
==================================================
downloaded 237 Kb
trying URL 'http://cran.rstudio.com/src/contrib/Rcpp_0.11.1.tar.gz'
Content type 'application/x-gzip' length 2003515 bytes (1.9 Mb)
opened URL
==================================================
downloaded 1.9 Mb
trying URL 'http://cran.rstudio.com/src/contrib/stringr_0.6.2.tar.gz'
Content type 'application/x-gzip' length 20636 bytes (20 Kb)
opened URL
==================================================
downloaded 20 …Run Code Online (Sandbox Code Playgroud) 尝试rvest在R中使用包时收到以下错误:
open.connection(x,"rb")出错:无法连接到服务器
是什么导致此错误消息?功能如下所示:
htmlpage <- read_html("http://forecast.weather.gov/MapClick.php?lat=42.27925753000045&lon=-71.41616624299962#.V17UH-IrKHs")
Run Code Online (Sandbox Code Playgroud)