我知道在这个网站上有类似的问题,但是,他们似乎都没有充分回答我的问题.
这是我到目前为止所做的:
我有一个csv文件,我在excel中打开.我以代数方式操纵列以获得新的列"A".我将文件导入R使用read.csv()
,并将A列中的条目存储为因子 - 我希望它们存储为数字.我在这个主题上找到了这个问题:
根据建议,我stringsAsFactors = FALSE
作为参数包含read.csv()
,但是,正如Hong Ooi在上面链接的页面中建议的那样,这不会导致A列中的条目存储为数值.
可能的解决方案是使用以下页面中给出的建议:
但是,我想要一个更清晰的解决方案,即导入文件的方法,以便列条目的条目存储为数值.
欢呼任何帮助!
今天我已经意识到包装中的silhouette
图形cluster
无法正确显示RStudio
.Google搜索显示其他人遇到此问题:
http://support.rstudio.org/help/discussions/problems/3094-plotsnot-showing-up-in-r-studio
作为R的新手,我不清楚这个问题是否已在这个问题得到解决!所以我的问题是:有没有办法让silhouette
情节正确显示RStudio
?
谢谢你的帮助.
示例脚本:
library(cluster)
data(xclara)
km <- kmeans(xclara,3)
dissE <- daisy(xclara)
sk <- silhouette(km$cl, dissE)
plot(sk)
Run Code Online (Sandbox Code Playgroud) 我有一些数据dt = data.table(x=c(1:200),y=rnorm(200))
,我开始使用密度图ggplot2
:
plot = ggplot(dt,aes(y)) + geom_density(aes(y=..density..))
Run Code Online (Sandbox Code Playgroud)
有没有办法可以添加类似于此的百分位线?
如果进一步的我能遮荫的图形相似(由百分线创建)的段这个,那么这将是伟大的!
在使用ggplot2获得闪亮的基本功能之后,我正在尝试使用rCharts.但是,我无法显示人力车图.任何帮助非常感谢; 放轻松 - 我只是习惯了这个;)
### ui
library(shiny)
require(devtools)
install_github('rCharts', 'ramnathv')
# moved from lower down so call to `showOutput` would not fail
library(rCharts)
library(rattle)
shinyUI(
pageWithSidebar(
headerPanel("Rickshaw test"),
sidebarPanel(
selectInput("variable",
"Choice is arbitrary:",
c("Choice 1", "Choice 2")
)
),
mainPanel(
showOutput("plot", "Rickshaw")
)
)
)
### server
data(weather)
w = weather
dateToSeconds = function (date) {
date = as.POSIXct(as.Date(date), origin = "1970-01-01")
as.numeric(date)
}
w$Date = dateToSeconds(w$Date)
shinyServer(function(input, output) {
output$mpgPlot = renderChart({
rs = Rickshaw$new()
rs$layer(MinTemp ~ Date,
data …
Run Code Online (Sandbox Code Playgroud) 我需要一个函数,它将一个新列(具有常量值)添加到数据帧df
.到目前为止我的尝试是这样的:
f = function(df, col.name, col.value){
df$col.name = col.value
print(df)
}
Run Code Online (Sandbox Code Playgroud)
典型的输入是:
f(df, "New column", NA)
Run Code Online (Sandbox Code Playgroud)
这会给我一个值为NA的新列,但它会被命名col.name
.
任何帮助非常感谢.
我有一个函数,它将数据帧和其他参数作为输入,这些参数确定数据帧的某种类型的更改.举个简单的例子:
col_with_ones <- function(df, col_name) {
df[[col_name]] <- 1
df
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以使用Reduce
(或任何其他"高级"功能)将多个更改应用于数据帧?例如,继续上面的例子,我可以Reduce
用来做以下事情:
df <- data.frame(a = runif(10))
for (letter in letters[2:5]) {
df <- col_with_ones(df, letter)
}
Run Code Online (Sandbox Code Playgroud)
干杯
我认为以下一点代码为这个问题提供了动力:
add <- function(x, y = 1) x + y
subtract <- function(x, z = 1) x - z
both <- function(x, ...) list(add(x, ...), subtract(x, ...))
both(1)
# OK
both(1, z = 2)
# Error in add(x, ...) : unused argument (z = 2)
# a solution from a previous question I asked
add <- function(x, y = 1, ...) x + y
subtract <- function(x, z = 1, ...) x - z
both(1, z = 2)
Run Code Online (Sandbox Code Playgroud)
所以我正在寻找一个名为add_dots …
这是我到目前为止的地方:
我有一个数据框,df
有两列A
和B
(都包含实数)b
依赖a
.我将列相互绘制:
p = ggplot(df, aes(A, B)) + geom_point()
Run Code Online (Sandbox Code Playgroud)
并且看到这种关系是非线性的.添加:
p = p + geom_smooth(method = 'loess', span = 1)
Run Code Online (Sandbox Code Playgroud)
给出了"良好"的最佳匹配线.给定I 的新值a
,A
然后使用以下方法来预测值B
:
B.loess = loess(B ~ A, span = 1, data = df)
predict(B.loess, newdata = a)
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.然而,我意识到我无法推断使用loess
(大概是因为它是非参数的?!).推断看起来相当自然 - 这种关系看起来像是一种强大的东西,例如:
x = c(1:10)
y = 2^x
df = data.frame(A = x, B = y)
Run Code Online (Sandbox Code Playgroud)
这是我失去的地方.首先,我可以使用哪些方法来绘制最适合这种('power')数据的线而不使用loess
?可悲的尝试,例如:
p = ggplot(df, aes(A, B)) …
Run Code Online (Sandbox Code Playgroud)