[英]Create new data frame with column names as row names, and values from one column as new column names
[英]Create New Data Frame with Column Names from Unique Values in another Data Frame and Corresponding Values Assigned to Column
我是R的新手,我很確定這很容易實現,但我無法弄清楚如何執行此操作。 我已經嘗試了使用for循環的split功能,但無法弄清楚如何正確使用它。 例如,這就是我原始數據框的樣子:
dat <- data.frame(col1 = c(rep("red", 4), rep("blue", 3)), col2 = c(1, 3, 2, 4, 7, 8, 9))
col1 col2
red 1
red 3
red 2
red 4
blue 7
blue 8
blue 9
我想為col1中的每個唯一值創建新列,並將它在col2中的corressponding值分配給新數據框。 這就是我想要的新數據框架:
red blue
1 7
3 8
2 9
4 NA
我已經接近了一個接近我想要的列表結構,但是我需要一個數據框來進行boxplot和dotplot結果。 任何幫助都會得到滿足。 謝謝!
我確信這是一個更有效的解決方案,但這里有一個選擇
dat <- data.frame(col1 = c(rep("red", 4), rep("blue", 3)), col2 = c(1, 3, 2, 4, 7, 8, 9))
dat
col1 col2
1 red 1
2 red 3
3 red 2
4 red 4
5 blue 7
6 blue 8
7 blue 9
ust <- unstack(dat, form = col2 ~ col1)
res <- data.frame(sapply(ust, '[', 1:max(unlist(lapply(ust, length)))))
res
blue red
1 7 1
2 8 3
3 9 2
4 NA 4
編輯:如果您希望列順序為紅色,則為藍色
res[, c("red", "blue")]
red blue
1 1 7
2 3 8
3 2 9
4 4 NA
這是一個Hadleyverse可能的解決方案
library(tidyr)
library(dplyr)
dat %>%
group_by(col1) %>%
mutate(n = row_number()) %>%
spread(col1, col2)
# Source: local data frame [4 x 3]
#
# n blue red
# 1 1 7 1
# 2 2 8 3
# 3 3 9 2
# 4 4 NA 4
或者使用data.table
library(data.table)
dcast(setDT(dat)[, indx := 1:.N, by = col1], indx ~ col1, value.var = "col2")
# indx blue red
# 1: 1 7 1
# 2: 2 8 3
# 3: 3 9 2
# 4: 4 NA 4
只是為了顯示使用base R *apply
和cbind
另一個選項
# split the data into list using col1 column
tmp.list = lapply(split(dat, dat$col1), function(x) x$col2)
# identify the length of the biggest list
max.length = max(sapply(tmp.list, length))
# combine the list elements, while filling NA for the missing values
data.frame(do.call(cbind,
lapply(tmp.list, function(x) c(x, rep(NA, max.length - length(x))))
))
# blue red
#1 7 1
#2 8 3
#3 9 2
#4 NA 4
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.