R-如何仅相互关联某些行
我有一个数据帧,它简化为:R-如何仅相互关联某些行,r,dataframe,multiplication,R,Dataframe,Multiplication,我有一个数据帧,它简化为: x <- data.frame( condition = c("ctrl", "ctrl", "ctrl", "ctrl", "exp", "exp", "exp", "exp"), type = c(1, 2, 3, 4, 1, 2, 3, 4), value = c("x", "x", "x", "x", "x", "x", "x", "x") ) # condition type value # 1 ctrl 1 x
x <- data.frame(
condition = c("ctrl", "ctrl", "ctrl", "ctrl", "exp", "exp", "exp", "exp"),
type = c(1, 2, 3, 4, 1, 2, 3, 4),
value = c("x", "x", "x", "x", "x", "x", "x", "x")
)
# condition type value
# 1 ctrl 1 x
# 2 ctrl 2 x
# 3 ctrl 3 x
# 4 ctrl 4 x
# 5 exp 1 x
# 6 exp 2 x
# 7 exp 3 x
# 8 exp 4 x
我想创建一个新列,它是“类型1的值”乘以“类型2的值”。有没有人对这方面的最佳方法有什么建议?我相信其他人会有一个更优雅的解决方案,但我的建议是:将数据从长格式扩展到宽格式,将所需的列成倍增加,然后将其全部收集回长格式
# Load packages
library(dplyr)
library(tidyr)
# Make dataframe
df <- data_frame(condition = rep(c('ctrl', 'exp'), each = 4),
type = as.character(rep(1:4, times = 2)),
value = rnorm(8))
# Print df
df
#> # A tibble: 8 × 3
#> condition type value
#> <chr> <chr> <dbl>
#> 1 ctrl 1 0.38735743
#> 2 ctrl 2 0.04950654
#> 3 ctrl 3 0.23559332
#> 4 ctrl 4 -0.02618723
#> 5 exp 1 0.77968387
#> 6 exp 2 -1.28652883
#> 7 exp 3 0.99731983
#> 8 exp 4 -0.28059754
# Process df
df_2 <- df %>%
# Retain types 1 and 2
filter(type == 1 | type == 2) %>%
# Spread the type column
spread(key = type,
value = value) %>%
# Multiply values in type `1` and `2`
mutate(`1 * 2` = `1` * `2`) %>%
# Gather the types back together
# (omiting condition and `1 * 2` from the gather)
gather(key = type,
value = value,
-c(`1 * 2`, condition)) %>%
# Reorder columns
select(condition, type, value, `1 * 2`)
# Print df_2
df_2
#> # A tibble: 4 × 4
#> condition type value `1 * 2`
#> <chr> <chr> <dbl> <dbl>
#> 1 ctrl 1 0.38735743 0.01917673
#> 2 exp 1 0.77968387 -1.00308578
#> 3 ctrl 2 0.04950654 0.01917673
#> 4 exp 2 -1.28652883 -1.00308578
如果您想将所有数据帧重新组合在一起,这样您就拥有了所有可能的“类型”,然后将这两个数据帧连接起来
# Join df_2 and df
df_3 <- df %>%
left_join(df_2)
#> Joining, by = c("condition", "type", "value")
# Print df_3
df_3
#> # A tibble: 8 × 4
#> condition type value `1 * 2`
#> <chr> <chr> <dbl> <dbl>
#> 1 ctrl 1 0.38735743 0.01917673
#> 2 ctrl 2 0.04950654 0.01917673
#> 3 ctrl 3 0.23559332 NA
#> 4 ctrl 4 -0.02618723 NA
#> 5 exp 1 0.77968387 -1.00308578
#> 6 exp 2 -1.28652883 -1.00308578
#> 7 exp 3 0.99731983 NA
#> 8 exp 4 -0.28059754 NA
不是我的反对票,但这不清楚,你说“类型1的值”乘以“类型2的值”,但你的值是x,不是数字。这似乎只是两个值——一个用于condition=ctrl,另一个用于condition=exp——而不是一列。请尽量弄清楚你想得到什么样的答案。我想你是在寻找一个一般情况。请更新您的示例,使其更通用。对不起,值是数字,但数据集非常复杂。是的,在这种情况下只有2个值,但它要大得多。我不知道还能怎么解释。谢谢!可能是因为我的数据帧,但是“排列”创建了两列,其中一半的行都有NA。“有没有办法把这些结合起来?”克里兹不知道你的意思。请张贴一个可复制的例子。