我有以下数据:
library(leaflet)
library(leaflet.extras)
library(flexdashboard)
library(htmltools)
library(htmlwidgets)
library(plotly)
id = 1:1000
long = 2.2945 + rnorm( 1000, 0.1085246 , 0.1)
lat = 48.8584 + rnorm( 1000, 0.009036273 , 0.1)
my_data_1 = data.frame(id, lat, long)
id = 1:1000
long = 2.2945 + rnorm( 1000, 0.1085246 , 0.1)
lat = 48.8584 + rnorm( 1000, 0.009036273 , 0.1)
my_data_2 = data.frame(id, lat, long)
Run Code Online (Sandbox Code Playgroud)
使用这些数据,我制作了这些地图:
map1 = my_data_1 %>%
leaflet() %>%
addTiles() %>%
addHeatmap(lng=~long,lat=~lat,max=100,radius=20,blur=10)
map2 = my_data_2 %>%
leaflet() %>%
addTiles() %>%
addHeatmap(lng=~long,lat=~lat,max=100,radius=20,blur=10)
map3 = …
Run Code Online (Sandbox Code Playgroud) 在 R 编程语言中,我对执行“模糊连接”并通过 SQL 连接传递它感兴趣:
library(fuzzyjoin)
library(dplyr)
library(RODBC)
library(sqldf)
con = odbcConnect("some name", uid = "some id", pwd = "abc")
sample_query = sqlQuery( stringdist_inner_join(table_1, table_2, by = "id2", max_dist = 2) %>%
filter(date_1 >= date_2, date_1 <= date_3) )
view(sample_query)
Run Code Online (Sandbox Code Playgroud)
但是,我认为这是不可能的,因为不支持我们用于“模糊连接”(stringdist_inner_join)的函数。
我试图找到这个“模糊连接”函数的源代码,并在这里找到它: https: //rdrr.io/cran/fuzzyjoin/src/R/stringdist_join.R
我的问题:有谁知道是否可以(手动)将此“模糊连接”函数转换为可识别的 SQL 格式?有没有快速的方法来重写这个函数(stringdist_inner_join),以便它可以被Netezza识别?是否有任何现有的方法可以做到这一点?
现在我只能在本地执行“sample_query” - 重写此函数(stringdist_inner_join)将使执行“sample_query”更快。
有谁知道这是否可能?
笔记:
我的数据如下所示:
table_1 = data.frame(id1 = c("123 A", "123BB", "12 5", "12--5"), id2 = c("11", "12", "14", "13"),
date_1 = c("2010-01-31","2010-01-31", "2015-01-31", "2018-01-31" ))
table_1$id1 = as.factor(table_1$id1)
table_1$id2 …
Run Code Online (Sandbox Code Playgroud) 我有以下情况:
如果该马尔可夫链从位置 = 5 开始,则变量在 prob=0.5 时移动 +1,在 prob=-0.5 时移动 -1
我尝试按如下方式执行此操作(我创建了一个跟踪变量来查看模拟卡在哪里):
# the simulations can take a long time to run, I interrupted them
library(ggplot2)
library(gridExtra)
n_sims <- 100
times_to_end_0 <- numeric(n_sims)
times_to_end_0_after_10 <- numeric(n_sims)
paths_0 <- vector("list", n_sims)
paths_0_after_10 <- vector("list", n_sims)
for (i in 1:n_sims) {
print(paste("Running simulation", i, "for situation 1..."))
y <- 5
time <- 0
path_0 <- c(y)
while(y > 0) {
step <- sample(c(-1, …
Run Code Online (Sandbox Code Playgroud) 我模拟了一些数据并使用 visnetwork 在 R 中创建了一个图形网络:
library(igraph)
library(dplyr)
library(visNetwork)
#create file from which to sample from
x5 <- sample(1:100, 1100, replace=T)
#convert to data frame
x5 = as.data.frame(x5)
#create first file (take a random sample from the created file)
a = sample_n(x5, 1000)
#create second file (take a random sample from the created file)
b = sample_n(x5, 1000)
#combine
c = cbind(a,b)
#create dataframe
c = data.frame(c)
#rename column names
colnames(c) <- c("a","b")
#create graph
graph <- graph.data.frame(c, directed=F)
graph <- …
Run Code Online (Sandbox Code Playgroud) 最近,我读到古巴比伦文明使用以 60 为基数而不是 10 为基数的数字系统。即使使用以 60 为基数的数字系统,他们仍然能够近似计算 2 \xe2\x80\x94 的平方根,并且几千年前也是如此!
\n\n我对此很好奇,想看看如何将十进制(以 10 为基数)中的数字转换为六十进制(以 60 为基数)。使用 R 编程语言,我找到了此链接,其中提供了有关将数字从某个基数转换为不同基数的答案。
\n然而,这里似乎基数只能在 2 到 36 之间(我想要基数 60):
\nbase <- function(b, base = 10)\n{\n base <- as.integer(base)\n if(base > 36 | base < 2) stop("\'base\' must be between 2 and 36.")\n \n structure(lapply(b, function(x) \n {\n n <- ceiling(log(x, base))\n vec <- numeric()\n val <- x\n \n while(n >= 0)\n {\n rem <- val %/% base^n\n val …
Run Code Online (Sandbox Code Playgroud) 我正在使用 R 编程语言。我正在尝试在具有不平衡类(即二元响应变量比率 95% 到 5%)的非常大的数据集(超过 1 亿行)上拟合随机森林模型。为此,我编写了 R 代码:
下面,我包含了这些步骤的 R 代码:
第 1 步:为问题创建数据
# Step 1: Randomly create data and make initial training/test set:
library(dplyr)
library(ranger)
original_data = rbind( data_1 = data.frame( class = 1, height = rnorm(10000, 180,10), weight = rnorm(10000, 90,10), salary = rnorm(10000,50000,10000)), data_2 = data.frame(class = 0, height = rnorm(100, 160,10), weight = rnorm(100, …
Run Code Online (Sandbox Code Playgroud) parallel-processing loops r machine-learning cluster-computing
使用 R 中的“plotly”库 - 我生成了一些随机数据并进行了一些交互式数据可视化:
library(plotly)
library(ggplot2)
library(dplyr)
library(hrbrthemes)
#subplot 1
data1 <- data.frame(
day = as.Date("2017-06-14") - 0:364,
value = runif(365) - seq(-140, 224)^2 / 10000
)
p1 <- ggplot(data1, aes(x=day, y=value)) +
geom_line( color="#69b3a2") +
xlab("") +
theme_ipsum() +
theme(axis.text.x=element_text(angle=60, hjust=1))
fig1 <- ggplotly(p1)
scatter_1 = data.frame(x = rnorm(100,100,100), y = rnorm(100,100,100))
fig2 <- plot_ly(data = scatter_1, x = ~x, y = ~y)
#subplot 2
data2 <- data.frame(
day = as.Date("2017-06-14") - 0:364,
value = runif(365) - seq(-140, …
Run Code Online (Sandbox Code Playgroud) 我遇到了以下逻辑问题:
在此问题中,您需要将篮球运动员的真实姓名与其昵称相匹配,并按身高对篮球运动员进行排序。通常,这个问题需要你手动枚举名字-昵称和名字-身高的不同组合,直到根据下面的条件不存在矛盾为止。
我想知道是否可以使用 R 等编程语言通过暴力破解来解决此类问题。
例如,下面的代码按身高列出了篮球运动员的所有可能组合:
my_list = c("Bill", "Ernie", "Oscar", "Sammy", "Tony")
d = permn(my_list)
all_combinations = as.data.frame(matrix(unlist(d), ncol = 120)) |>
setNames(paste0("col", 1:120))
data_frame_version = data.frame(matrix(unlist(d), ncol = length(d))
matrix_version = matrix(unlist(d), ncol = length(d))
#first 20 rows of matrix version:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19]
[1,] "Bill" "Bill" "Bill" "Bill" "Tony" "Tony" "Bill" "Bill" "Bill" "Bill" "Bill" "Bill" "Bill" "Bill" "Tony" "Tony" "Sammy" …
Run Code Online (Sandbox Code Playgroud) 假设我有以下因素列表:
factor_1 = c("A1", "A2", "A3")
factor_2 = c("B1", "B2")
factor_3 = c("C1", "C2", "C3", "C4")
factor_4 = c("D1", "D2", "D3")
Run Code Online (Sandbox Code Playgroud)
我制作了以下数据框,其中包含这些因素的所有 (3 * 2 * 4 * 3 = ) 72 种组合:
data_exp <- expand.grid(factor_1, factor_2, factor_3, factor_4)
data_exp$id = 1:nrow(data_exp)
> head(data_exp)
Var1 Var2 Var3 Var4 id
1 A1 B1 C1 D1 1
2 A2 B1 C1 D1 2
3 A3 B1 C1 D1 3
4 A1 B2 C1 D1 4
5 A2 B2 C1 D1 5
6 …
Run Code Online (Sandbox Code Playgroud) 我正在使用 R 编程语言。
假设有 100 人 - 每个人都用 1:100 开始的 ID 表示。每个人都可以与其他人成为朋友。数据集可以用图形/网络格式表示,如下所示:
# Set the seed for reproducibility
set.seed(123)
# Generate a vector of ID's from 1 to 100
ids <- 1:100
# Initialize an empty data frame to store the "from" and "to" values
edges <- data.frame(from=integer(), to=integer(), stringsAsFactors=FALSE)
# Iterate through the ID's
for(id in ids) {
# Randomly select a minimum of 1 and a maximum of 8 neighbors for the current ID
neighbors <- sample(ids[ids != …
Run Code Online (Sandbox Code Playgroud) r ×10
graph ×2
html ×2
igraph ×2
algorithm ×1
decimal ×1
integer ×1
interactive ×1
join ×1
list ×1
loops ×1
performance ×1
plotly ×1
popup ×1
r-markdown ×1
random ×1
random-walk ×1
sorting ×1
sql ×1
visnetwork ×1