[英]Combine values of data.frame
I'm R-beginner, I assume this is not so difficult. 我是R初学者,我认为这并不困难。 I want to combine values of a
data.frame
, like this: 我想结合
data.frame
值,像这样:
Input data.frame: 输入数据帧:
col1 col2
1 a 50
2 a 80
3 b 40
4 c 20
Output data.frame: 输出data.frame:
col1 col1
1 a [50,80]
2 b [40]
3 c [20]
In the input, col1
is not unique. 在输入中,
col1
不是唯一的。 For each value in col1
, I want to combine all values in col2
in a vector. 对于
col1
每个值,我想将col2
中的所有值合并到一个向量中。 In the output, col1
is unique. 在输出中,
col1
是唯一的。
Can you help me with this? 你能帮我吗?
Entries of a data-frame cannot be lists, they must be atomic. 数据框的条目不能是列表,它们必须是原子的。 What you probably want is to get a named list of vectors:
您可能想要的是获取向量的命名列表:
df <- data.frame( col1 = c('a','a','b','c'), col2 = c(50,80,40,20))
with(df, tapply(col2, col1, list))
which is a named list: 这是一个命名列表:
$a
[1] 50 80
$b
[1] 40
$c
[1] 20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.