我必须使用R创建距离矩阵.我的数据在excel文件中,包含300行和10列.我必须根据第9列的值创建距离矩阵.例如
s s s s s
s 1
s 2 2
s 3 3 4
s 4 4 7 3
s 5 5 8 2 8
Run Code Online (Sandbox Code Playgroud)
如何创建这种类型的矩阵?
输入是一个正方形矩阵,其中大部分0和一部分1。目标是1沿输入矩阵的对角线获取连续s 的(某种)累积和。
#Input
ind = rbind(cbind(x = c(2, 3, 1, 2 , 3),
y = c(1, 2, 3, 4, 5)))
m1 = replace(matrix(0, 5, 5), ind, 1)
m1
# [,1] [,2] [,3] [,4] [,5]
#[1,] 0 0 1 0 0
#[2,] 1 0 0 1 0
#[3,] 0 1 0 0 1
#[4,] 0 0 0 0 0
#[5,] 0 0 0 0 0
#Desired Output
# [,1] [,2] [,3] [,4] [,5]
# [1,] …Run Code Online (Sandbox Code Playgroud) 我沿着这个线创建了一个情节:
http://www.buildingwidgets.com/blog/2015/7/2/week-26-sunburstr
# devtools::install_github("timelyportfolio/sunburstR")
library(sunburstR)
# read in sample visit-sequences.csv data provided in source
# https://gist.github.com/kerryrodden/7090426#file-visit-sequences-csv
sequence_data <- read.csv(
paste0(
"https://gist.githubusercontent.com/kerryrodden/7090426/"
,"raw/ad00fcf422541f19b70af5a8a4c5e1460254e6be/visit-sequences.csv"
)
,header=F
,stringsAsFactors = FALSE
)
Run Code Online (Sandbox Code Playgroud)
在 Rstudio 中,我可以在查看器中单击:“导出 > 另存为网页...”
然后将绘图保存为交互式 html 文档。我想将此作为代码的一部分。如何使用 R 代码将绘图保存到 html?PDF/jpg 等有很多示例,但没有 html。
我有以下R代码用于简单绘图:
ExperimentDataNames = c('Count', 'HumanData', 'ActualPrices')
ExperimentData <- read_csv("/Users/justin_chudley/desktop/ExperimentData.csv", col_names = ExperimentDataNames)
x <- Count <- ExperimentData$Count
y <- HumanData <- ExperimentData$HumanData
y1 <- ActualPrices <- ExperimentData$ActualPrices
plot(x,y, type = "l", xlab="Trial Number",ylab="$USD",main="Dow Jones Price vs Human Experiment")
lines(x,y1, type = "l", col=2)
legend=c('Human Data', 'Actual Prices')
Run Code Online (Sandbox Code Playgroud)
为什么我的传说没有显示?
我有一个长矩阵,我想填写,rnorm(1)但它需要很长时间(不像下面的示例).是否有另一种方法,因为行数和列数总是相等但是动态的.
my <- matrix(c(0), nrow= 3, ncol = 3)
for (i in 1:3){
for (j in 1:3){
my[i,j]<-rnorm(1)
}
}
Run Code Online (Sandbox Code Playgroud)