[英]How do I create variables in R that count the number of values of one variable by another variable?
[英]How do I create two new variables out of one variable, and attach dummy values to it in R?
我對任何類型的編碼都是全新的,尤其是 R,所以我在谷歌上搜索的日子並不是很有幫助。 我真的很感激任何幫助/見解!
我想知道如何從原始變量中獲取兩個新變量,並為其附加新值 - 基本上我從這個開始:
並想得到這個:
我設法用melt(dataname, id.vars=c("ID"))
得到它的長格式,我得到的 ID 和值很好。 但是只有一個變量,我的四個標題(loudHot、quietHot、loudCold、quietCold)重復了 - 我如何從中創建兩個新變量並為其分配值(例如,“Volume”的值是 1 當原始變量是 loudHot 或 loudCold,如果是 quietHot 或 quietCold,則為 0,然后當原始變量為 loudHot 或 quietHot 時,“Temp”為 1,當它的 loudCold 或 quietCold 時為 0)?
我不會對自己太苛刻 - 這並不是微不足道的。 無論如何,您可以使用pivot_longer
中的tidyr
和dplyr
進行一些數據操作來實現您想要的結果:
library(dplyr)
library(tidyr)
df %>%
pivot_longer(-ID) %>%
mutate(Volume = as.numeric(grepl("loud", name)),
Temp = as.numeric(grepl("Hot", name))) %>%
select(ID, Volume, Temp, value)
#> # A tibble: 32 x 4
#> ID Volume Temp value
#> <dbl> <dbl> <dbl> <dbl>
#> 1 2 1 1 14
#> 2 2 0 1 16
#> 3 2 1 0 16
#> 4 2 0 0 15
#> 5 4 1 1 19
#> 6 4 0 1 15
#> 7 4 1 0 10
#> 8 4 0 0 8
#> 9 6 1 1 11
#> 10 6 0 1 17
#> # ... with 22 more rows
數據
df <- data.frame(ID = (1:8) * 2,
loudHot = c(14, 19, 11, 20, 18, 17, 16, 2),
quietHot = c(16, 15, 17, 5, 10, 10, 15, 0),
loudCold = c(16, 10, 10, 4, 3, 2, 14, 2),
quietCold = c(15, 8, 17, 8 ,10, 12, 5, 0))
作為任何未來 SO 問題的提示,請不要發布數據圖像。 這里的人們需要能夠剪切和粘貼數據文本以測試和驗證解決方案。 理想情況下,您應該通過將輸入 function 的dput
放入代碼塊中來執行此操作。 人們很少會從圖像中手動轉錄數據。
由代表 package (v2.0.1) 於 2022 年 2 月 4 日創建
以免使用dplyr
和tidyr
包來解決您的問題。
對您的第一個建議是始終添加您的數據的最小可重現示例,以便我們使用它並更快地幫助您。 這並不復雜,您可以使用dput(head(yourdata, 10))
,例如,或模擬一些觀察。
我做了一個模擬如下:
library(dplyr)
library(tidyr)
data <- data.frame(
id = 1:5,
loudHot = sample(10:20, 5, replace = TRUE),
quieHot = sample(10:20, 5, replace = TRUE),
loudCold = sample(0:12, 5, replace = TRUE),
quiteCold = sample(0:12, 5, replace = TRUE)
)
現在我們有了數據,不要使用tidyr::pivot_longer
將其轉換為長格式。 此 function 將 dataframe 以寬格式作為參數,您想要收集的列(或您不想使用-
符號收集的列)。
# Data to long format
data_long <- pivot_longer(
data, cols = -id,
names_to = 'variable', values_to = 'value'
)
有了它,現在您只需要創建虛擬對象,這很簡單。
# Adding new variables
data_with_dummy <- mutate(
data_long,
volume = as.numeric(variable %in% c('loudHot', "loudCold")),
temp = as.numeric(variable %in% c('loudHot', "quietCold"))
)
這是一個基本的 R 方法:
# Original data
df <- data.frame(
ID = c(2, 4, 5, 7, 8, 11, 12, 16),
loudHot = c(14, 19, 11, 20, 18, 17, 16, 2),
quietHot = c(16, 15, 17, 5, 10, 10, 15, 0),
loudCold = c(16, 10, 10, 4, 3, 2, 14, 2),
quietCold = c(15, 8, 17, 8, 10, 12, 5, 0)
)
# Stacked data
df_stacked <- stack(
df,
select = c(
"loudHot", "quietHot", "loudCold", "quietCold"
)
)
# New variable for volume
df_stacked$Volume <- as.numeric(grepl("loud", df_stacked$ind))
# New variable for Temp
df_stacked$Temp <- as.numeric(grepl("Hot", df_stacked$ind))
# Replace "ind" values with "ID"
df_stacked$ind <- rep(df$ID, times = 4)
# Reorder columns
new_df <- df_stacked[,c(2:4,1)]
# Rename columns
colnames(new_df) <- c("ID", "Volume", "Temp", "Value")
# Order by ID
new_df[order(new_df$ID),]
我相信您的“音量”和“溫度”列應該是交替序列:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.