小编tcv*_*992的帖子

使用pivot_longer转换为更长的格式

我正在尝试使用 dplyr::pivot_longer 转换为更长的格式,但似乎无法让它执行我想要的操作。我可以使用 reshape::melt 进行管理,但我也希望能够使用pivot_longer 实现相同的目的。

我尝试重新格式化的数据是 mtcars 数据集的相关矩阵:

# Load packages
library(reshape2)
library(dplyr)

# Get the correlation matrix
mydata <- mtcars[, c(1,3,4,5,6,7)]
cormat <- round(cor(mydata),2)

head(cormat)
       mpg  disp    hp  drat    wt  qsec
mpg   1.00 -0.85 -0.78  0.68 -0.87  0.42
disp -0.85  1.00  0.79 -0.71  0.89 -0.43
hp   -0.78  0.79  1.00 -0.45  0.66 -0.71
drat  0.68 -0.71 -0.45  1.00 -0.71  0.09
wt   -0.87  0.89  0.66 -0.71  1.00 -0.17
qsec  0.42 -0.43 -0.71  0.09 -0.17  1.00
Run Code Online (Sandbox Code Playgroud)

然后,我只想过滤掉矩阵的上三角形;

#Get upper triangle of …
Run Code Online (Sandbox Code Playgroud)

pivot r reshape2 dplyr tidyverse

5
推荐指数
2
解决办法
758
查看次数

Pivot_longer 用于多列重复测量数据

我正在尝试使用包pivot_longer中的函数dplyr将我的数据转换为长格式。当前的广泛数据涉及 3 次重复测量患者的年龄、收缩压、是否使用降压药物 (med_hypt) 以及时间不变的“性别”变量。

示例数据和我尝试过的:

library(tidyverse)
library(dplyr)
library(magrittr)

wide_data <- structure(list(id = c(12002, 17001, 17002, 42001, 66001, 82002, 166002, 177001, 177002, 240001), 
                            sex = structure(c(2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L), 
                                            .Label = c("men", "women"), class = "factor"), 
                            time1_age = c(71.2, 67.9, 66.5, 57.7, 57.1, 60.9, 80.9, 59.7, 58.2, 66.6), 
                            time1_systolicBP = c(102, 152, NA_real_, 170, 151, 135, 162, 133, 131, 117), 
                            time1_med_hypt = c(0, 0, 0, 0, 0, 0, 0, 0, …
Run Code Online (Sandbox Code Playgroud)

pivot r dplyr tidyr

1
推荐指数
1
解决办法
3219
查看次数

绘制线性混合模型的预测值与观测值的关系图?

我对连续结果进行了重复测量,如下所示:

library(magrittr)
library(ggplot2)
library(nlme)
mydata <- structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 
7L, 7L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 11L, 11L, 11L, 
12L, 12L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 16L, 16L, 
17L, 17L, 17L, 17L, 17L, 18L, 18L, 19L, 19L, 20L, 20L, 21L, 21L, 
22L, 22L, 22L, 22L, 22L, 23L, 23L, 24L, …
Run Code Online (Sandbox Code Playgroud)

r lme4 mixed-models nlme

1
推荐指数
1
解决办法
1265
查看次数

使用 dplyr 根据 dplyr 中每组其他列中的条件添加列,数据采用长格式

我有 3 名患者的重复测量(4 或 5 次)的长格式数据:

library(dplyr)
library(magrittr)

questiondata <- structure(list(ID = c(2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 
4), time = c("time1", "time2", "time3", "time4", "time1", "time2", 
"time3", "time4", "time5", "time1", "time2", "time3", "time4", 
"time5"), drug_use = structure(c(NA, 1L, NA, NA, NA, 2L, NA, 
NA, NA, NA, 1L, NA, NA, NA), .Label = c("no", "yes"), class = "factor")), row.names = c(NA, 
-14L), class = c("tbl_df", "tbl", "data.frame"))

# Corresponding to the following tibble: …
Run Code Online (Sandbox Code Playgroud)

r case-when dplyr

0
推荐指数
1
解决办法
775
查看次数

在R中组合paste0和format时出现空格

为了显示我运行的回归结果,我有一个包含估计值和相应置信区间的小标题:

\n
library(tidyverse)\nlibrary(magrittr\n\nmydata <- structure(list(term = structure(c(1L, 3L, 4L), .Label = c("Intercept", \n"Follow-up time (years)", "Age (years)", "Sex (male)", "Never smoker (reference)", \n"Current smoker", "Former smoker", "Obesity (=30 kg/m\xc2\xb2)", "BMI (kg/m\xc2\xb2)", \n"Diabetes", "Glucose (mmol/L)", "Glucose lowering medication use", \n"Hypertension", "Systolic blood pressure (mmHg)", "Diastolic blood pressure (mmHg)", \n"Antihypertensive medication use", "Hypercholesterolemia", "LDL cholesterol (mmol/L)", \n"Lipid lowering medication use", "Chronic kidney disease (mL/min/1.73m\xc2\xb2)", \n"=90 (reference)", "60-89", "=60"), class = c("ordered", "factor"\n)), estimate = c(518.38, 0.98, 1.07), conf_low = c(178.74, 0.93, \n0.96), …
Run Code Online (Sandbox Code Playgroud)

string format r dplyr

0
推荐指数
1
解决办法
111
查看次数

标签 统计

r ×5

dplyr ×4

pivot ×2

case-when ×1

format ×1

lme4 ×1

mixed-models ×1

nlme ×1

reshape2 ×1

string ×1

tidyr ×1

tidyverse ×1