我有两个数据框,一个是调查数据(data.csv),另一个是标签数据(label.csv)。这是示例数据(我的原始数据大约有 150 个变量)
#sample data
df <- tibble::tribble(
~id, ~House_member, ~dob, ~age_quota, ~work, ~sex, ~pss,
1L, 4L, 1983L, 2L, 2L, 1, 1,
2L, 1L, 1940L, 7L, 2L, 1, 2,
3L, 2L, 1951L, 5L, 6L, 1, 1,
4L, 4L, 1965L, 2L, 2L, 1, 4,
5L, 3L, 1965L, 2L, 3L, 1, 1,
6L, 1L, 1951L, 3L, 1L, 1, 3,
7L, 1L, 1955L, 1L, 1L, 1, 3,
8L, 4L, 1982L, 2L, 2L, 2, 5,
9L, 2L, 1990L, 2L, 4L, 2, 3,
10L, 2L, …Run Code Online (Sandbox Code Playgroud) 我在 R 中有一个数据框。对于每一行,我想选择哪一列具有最高值,并粘贴该列的名称。当只有两列可供选择时,这很简单(请注意,如果两列的值都小于 0.1,我有一个不包括行的过滤步骤):
set.seed(6)
mat_simple <- matrix(rexp(200, rate=.1), ncol=2) %>%
as.data.frame()
head(mat_simple)
V1 V2
1 2.125366 6.7798683
2 1.832349 8.9610534
3 6.149668 15.7777370
4 3.532614 0.2355711
5 21.110703 1.2927119
6 2.871455 16.7370847
mat_simple <- mat_simple %>%
mutate(
class = case_when(
V1 < 0.1 & V2 < 0.1 ~ NA_character_,
V1 > V2 ~ "V1",
V2 > V1 ~ "V2"
)
)
head(mat_simple)
V1 V2 class
1 2.125366 6.7798683 V2
2 1.832349 8.9610534 V2
3 6.149668 15.7777370 V2
4 3.532614 …Run Code Online (Sandbox Code Playgroud) Data example.
date1 = seq(as.Date("2019/01/01"), by = "month", length.out = 29)
date2= seq(as.Date("2019/05/01"), by = "month", length.out = 29)
subproducts1=rep("1",29)
subproducts2=rep("2",29)
b1 <- c(rnorm(29,5))
b2 <- c(rnorm(29,5))
dfone <- data.frame("date"= c(date1,date2),
"subproduct"=
c(subproducts1,subproducts2),
"actuals"= c(b1,b2))
Run Code Online (Sandbox Code Playgroud)
Max Date for Subproduct 1 is May 2021 and max date for Subproduct 2 is Sept 2021.
Question: Is there a way to:
我有以下数据框:
df <- structure(list(GENE= c("ENS1", "ENS2",
"ENS3", "ENS4", "ENS1", "ENS2", "ENS3"), group= c(1L,
1L, 1L, 2L, 3L, 3L, 3L)),
class = "data.frame", row.names = c(NA, -7L))
GENE group
ENS1 1
ENS2 1
ENS3 1
ENS4 2
ENS1 3
ENS2 3
ENS3 3
Run Code Online (Sandbox Code Playgroud)
由于第 1 组和第 3 组相同,我想删除其中之一。我怎样才能做到这一点?
谢谢
此代码根据参与者和关系的数据帧绘制图表。
library(igraph)
actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David",
"Esmeralda"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
"David", "Esmeralda"),
to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
friendship=c(4,15,5,2,11,1))
g <- graph_from_data_frame(relations, directed=TRUE, vertices=actors)
plot(g)
Run Code Online (Sandbox Code Playgroud)
结果是:
我想根据 的值更改弧的厚度(而不是长度)relations$friendship。
我正在尝试创建一个路径序列。以下是示例数据集:
df <- structure(list(
sess_id = c(4, 4, 4, 4, 4, 4, 4, 7, 7, 7, 7, 7),
Page = c("A", "B", "C", "D", "A", "C", "B", "B", "C", "D", "A", "D")),
.Names = c("sess_id", "Page"),
row.names = c(NA, -12L),
class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
这是表:
| 会话ID | 页 |
|---|---|
| 4 | A |
| 4 | 乙 |
| 4 | C |
| 4 | D |
| 4 | A |
| 4 | C |
| 4 | 乙 |
| 7 | 乙 |
| 7 | C |
| 7 | D |
| 7 | A |
| 7 | D |
我想添加三列,如下所示:
| 会话ID | 页 | 小路 | 开始 | 结尾 |
|---|---|---|---|---|
| 4 | A | |||
| 4 | 乙 … |
我有一个包含这样的句子的小标题:
df <- tibble(sentences = c("Bob is looking for something", "Adriana has an umbrella", "Michael is looking at..."))
Run Code Online (Sandbox Code Playgroud)
另一个包含一长串名字:
names <- tibble(names = c("Bob", "Mary", "Michael", "John", "Etc."))
Run Code Online (Sandbox Code Playgroud)
我想查看句子是否包含列表中的名称,并添加一列来指示是否是这种情况,并获取以下 tibble :
wanted_df <- tibble(sentences = c("Bob is looking for something", "Adriana has an umbrella", "Michael is looking at..."), check = c(TRUE, FALSE, TRUE))
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试过,但没有成功:
df <- df %>%
mutate(check = grepl(pattern = names$names, x = df$sentences, fixed = TRUE))
Run Code Online (Sandbox Code Playgroud)
并且 :
check <- str_detect(names$names %in% df$sentences)
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助;)
我有一个 39x39 的成对相关性矩阵,其中包含 39 个个体的所有成对组合的相关性值。我想找到完全不相关的最大个体组,即该组中所有成对相关性值都等于 0。
在 R 中是否有一种简单的方法可以做到这一点?
一个更简单的例子:
set.seed(420)
#Create the matrix
relatedness.matrix <- matrix(data = sample(x = c(0.5, 1, 0,0), size = 25, replace = TRUE), nrow = 5, ncol = 5)
# Matrix has the same upper and lower triangles
relatedness.matrix[upper.tri(relatedness.matrix)] <- relatedness.matrix[lower.tri(relatedness.matrix)]
# Add names for simplicity of reference
colnames(relatedness.matrix) <- letters[1:5]
rownames(relatedness.matrix) <- letters[1:5]
# Relatedness between the same individual does not count
diag(relatedness.matrix) <- NA
Run Code Online (Sandbox Code Playgroud)
在这种情况下,存在三种可能的解决方案:仅包含 和 的 2x2 矩阵e、仅包含和 的 …
我在 R 中。我只想从 df1 中提取数字。我有例如:
df1 <- data.frame( column1 = c("Any[12, 15, 20]", "Any[22, 23, 30]"), column2 = c("Any[4, 17]", "Any[]"), stringsAsFactors = F )
我想要一个新的 df,它将括号内的整数乘以行号,并保留与其相对应的列信息。
例如 new_df 可能看起来像
| 时间 | 渠道 |
|---|---|
| 12 | 第1列 |
| 15 | 第1列 |
| 20 | 第1列 |
| 44 | 第1列 |
| 46 | 第1列 |
| 60 | 第1列 |
| 8 | 列2 |
| 34 | 列2 |
我不需要保留任何“NA”值,例如如果 Any[] 为空。有人知道这是否可能吗?我有大量这种格式的数据,所以我不能真正手动做很多事情。干杯!
我已经尝试过了:
new_df$Time <- as.integer(df1$column1)
那只是空白。
我也尝试过:
new_df$Time <- str_extract_all(new_df$Time, "\\d+" ) %>% lapply(function(x) as.integer(x)) %>% sapply(function(x) if.else(length(x) >0, x, NA) )
然后才返回每个括号内的第一个整数。例如
| 时间 | 渠道 |
|---|---|
| 12 | 第1列 |
| 44 | 第1列 |
| 8 … |
我有一个数据集,显示了X国甲方和乙方的宗教信仰,以及每个国家宗教信徒的百分比。
df <- data.frame(
PartyA = c("Christian","Muslim","Muslim","Jewish","Sikh"),
PartyB = c("Jewish","Muslim","Christian","Muslim","Buddhist"),
ChristianPop = c(12,1,74,14,17),
MuslimPop = c(71,93,5,86,13),
JewishPop = c(9,2,12,0,4),
SikhPop = c(0,0,1,0,10),
BuddhistPop = c(1,0,2,0,45)
)
# PartyA PartyB ChristianPop MuslimPop JewishPop SikhPop BuddhistPop
# 1 Christian Jewish 12 71 9 0 1
# 2 Muslim Muslim 1 93 2 0 0
# 3 Muslim Christian 74 5 12 1 2
# 4 Jewish Muslim 14 86 0 0 0
# 5 Sikh Buddhist 17 13 4 10 45
Run Code Online (Sandbox Code Playgroud)
借此,我想将“参与”的宗教信徒的总数加在一起。因此,第一行将得到一个等于 …