我有一个嵌套的 data.frames 列表,获取所有 data.frames 的列名的最简单方法是什么?
例子:
d = data.frame(a = 1:3, b = 1:3, c = 1:3)
l = list(a = d, list(b = d, c = d))
Run Code Online (Sandbox Code Playgroud)
结果:
$a
[1] "a" "b" "c"
$b
[1] "a" "b" "c"
$c
[1] "a" "b" "c"
Run Code Online (Sandbox Code Playgroud) 背景
这是一个玩具df:
df <- data.frame(ID = c("a","b","c","d","e","f"),
gender = c("f","f","m","f","m","m"),
zip = c(48601,NA,29910,54220,NA,44663),stringsAsFactors=FALSE)
Run Code Online (Sandbox Code Playgroud)
如您所见,我NA在zip列中有几个值。
问题
我正在尝试从中随机采样 2 整行df- 但我希望它们zip是不为空的行。
我试过的
这段代码为我提供了一个基本的(即无条件的)随机样本:
df2 <- df[sample(nrow(df), 2), ]
Run Code Online (Sandbox Code Playgroud)
但是,当然,这只能让我实现目标的一半——很多时候它会返回一个NA值为 in的行zip。此代码尝试添加条件:
df2 <- df[sample(nrow(df$zip != NA), 2), ]
Run Code Online (Sandbox Code Playgroud)
我想我已经接近了,但这会产生错误invalid first argument。
有任何想法吗?
我有以下名为 df 的示例数据框(dput如下):
group date indicator
1 A 2022-11-01 01:00:00 FALSE
2 A 2022-11-01 03:00:00 FALSE
3 A 2022-11-01 04:00:00 TRUE
4 A 2022-11-01 05:00:00 FALSE
5 A 2022-11-01 06:00:00 TRUE
6 A 2022-11-01 07:00:00 FALSE
7 A 2022-11-01 10:00:00 FALSE
8 A 2022-11-01 12:00:00 FALSE
9 B 2022-11-01 01:00:00 FALSE
10 B 2022-11-01 02:00:00 FALSE
11 B 2022-11-01 03:00:00 FALSE
12 B 2022-11-01 06:00:00 TRUE
13 B 2022-11-01 07:00:00 FALSE
14 B 2022-11-01 08:00:00 FALSE
15 B …Run Code Online (Sandbox Code Playgroud) 我有以下小题,
contact <- tribble(
~name, ~phone, ~email,
'John', 123, 'john_abc@gmail.com',
'John', 456, 'john_abc@gmail.com',
'John', 456, 'john_xyz@gmail.com',
'John', 789, 'john_pqr@gmail.com'
)
Run Code Online (Sandbox Code Playgroud)
如果电话或电子邮件相同,我想合并电话号码和电子邮件,所需的输出如下,
contact_combined <- tribble(
~name, ~phone, ~email,
'John', '123;456', 'john_abc@gmail.com;john_xyz@gmail.com',
'John', '789', 'john_pqr@gmail.com'
)
Run Code Online (Sandbox Code Playgroud)
我尝试先按姓名和电话进行分组,然后按姓名和电子邮件进行分组,但它没有给我预期的结果。我一直在寻找一种算法方法来解决这个问题,有人能给我建议吗?
注意:列中值的折叠不是这里的问题。这是关于选择要折叠的记录。
假设我们有一个M大小为n-by-的矩阵族n,它应该满足以下要求:
0或1,即布尔值,但对角线条目始终是0sM == t(M)p,使得all(rowSums(M)==p) == TRUEn由入度和出度都等于 的顶点组成的图的邻接矩阵p。这可以通过 来完成sample_degseq,但我们可能必须找到它的所有同构映射。如果我们使用方法,我们该如何做到这一点igraph?到目前为止,我的代码如下所示,但是当我们有更大的nor时,它会很慢p(而且我不确定在枚举过程中是否遗漏了一些矩阵)。
f <- function(n, p) {
# helper function to check if requirement holds
checker <- function(M, p, i = nrow(M) - 1) {
rs <- rowSums(M)
if ((i == nrow(M) - 1)) …Run Code Online (Sandbox Code Playgroud) 假设我们有一个方阵M,例如,
set.seed(1)
M <- matrix(rnorm(5*5), 5, 5)
> M
[,1] [,2] [,3] [,4] [,5]
[1,] -0.6264538 -0.8204684 1.5117812 -0.04493361 0.91897737
[2,] 0.1836433 0.4874291 0.3898432 -0.01619026 0.78213630
[3,] -0.8356286 0.7383247 -0.6212406 0.94383621 0.07456498
[4,] 1.5952808 0.5757814 -2.2146999 0.82122120 -1.98935170
[5,] 0.3295078 -0.3053884 1.1249309 0.59390132 0.61982575
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一种有效的方法可以找到一个子矩阵,使其行列式是所有子矩阵中的最大值。矩阵的大小应大于1x1但小于或等于5x5。一些子矩阵示例如下
> M[c(1,5),c(2,3)]
[,1] [,2]
[1,] -0.8204684 1.511781
[2,] -0.3053884 1.124931
> M[c(1,2,4),c(1,4,5)]
[,1] [,2] [,3]
[1,] -0.6264538 -0.04493361 0.9189774
[2,] 0.1836433 -0.01619026 0.7821363
[3,] 1.5952808 0.82122120 -1.9893517
> M[1:4,2:5] …Run Code Online (Sandbox Code Playgroud) 我使用以下命令:
table(factor("list",levels=1:"n")
Run Code Online (Sandbox Code Playgroud)
使用 "list": (example) a = c(1,3,4,4,3)
and levels = 1:5,还要考虑 2 和 5。对于非常大的数据集,我的代码似乎非常无效。
有没有人知道隐藏的库或代码片段以使其更快?
我对 R 比较陌生。我创建了代码来检查数据框并根据特定条件识别数据行,并用 1 和“检查”列标记这些行。该代码与测试数据完全按照我的预期工作。我的问题是真实的数据集有 100 万多行,虽然它可以工作,但速度太慢了。我希望能帮助提高这段代码的效率。
#create test data
alarm <- c(0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0)
setpoint <- c(10,10,10,10,10,10,10,10,8,8,9,8,8,10,10,10,10,10,10,10,10,10,10,10,8,10,10,8,10,10,10)
temp <- data.frame(alarm, setpoint)
#create a new column to capture if there is any changes to setpoint after any alarm
temp$check <- ""
#review everyrow in dataframe
for(i in 1:nrow(temp)){
cat(round(i/nrow(temp)*100,2),"% \r") # prints the percentage complete in realtime.
if(temp$alarm[i]==1 && temp$setpoint[i] >= 10){
#for when alarm has occurred and the setpoint is 10 or above review the next 5 rows
for(j in 0:5){ …Run Code Online (Sandbox Code Playgroud) 使用 R,如何生成位于和 之间的n随机数x_1, ...,其总和为?x_n-110
推广到另一个总和和另一个范围怎么样?
我有一个只有三个单词的字符串,如下所示:
first_string <- c("self", "funny", "nymph")
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,这个向量的单词可以全部组合成一个单词,因为字母中有一些重叠,即我们得到 self fun un ny mph。我们称其为单词列车。
此外,我还有另一个包含很多单词的向量。设第二个向量为:
second_string <- c("house", "garden", "duck", "evil", "fluff")
Run Code Online (Sandbox Code Playgroud)
我想知道第二个字符串的哪些单词可以添加到单词序列中。在这种情况下,这是houseand fluff(可以添加在 self fun ny mphhouse的末尾,并且可以放在and之间)。所以这里的预期输出是:fluffselffunny
expected <- data.frame(word= c("house", "fluff"), word_train= c("selfunnymphouse", "selfluffunnymph"))
Run Code Online (Sandbox Code Playgroud)
重叠可以是任意长度,即自我和滑稽仅与一个角色重叠,但滑稽和若虫在两个角色中重叠。
编辑
新词可以改变第一个词串的词序。例如,如果第二个向量包含单词,hugs我们可以将单词 train nymp h ug s el funny放在和nymph之前。selffunny
r ×10
algorithm ×3
dataframe ×3
performance ×3
igraph ×2
matrix ×2
random ×2
character ×1
combinations ×1
count ×1
cvxr ×1
data.table ×1
datetime ×1
determinants ×1
dplyr ×1
for-loop ×1
nested ×1
optimization ×1
overlap ×1
string ×1
subset ×1
tabulate ×1