本文介绍了通过将值与列名匹配来填充 data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有两个 data.frames:

I have two data.frames:

df1 (空,但具有特定的姓氏)

df1 (empty, but with specific colnames)

apple orange banana pear grape
0      0       0       0     0

df2

fruit1  count1 fruit2 count2
apple   2      pear   1
grape  4      orange 2
banana 1      NA     NA

这是我想要的输出:

apples oranges bananas pears grapes
2      0       0       1     0
0      2       0       0     4
0      0       1       0     0

我考虑过采取以下措施:

I've considered doing something along the lines of:

for f (in range(nrow(df2))){
  for (i in range(ncol(df1))){
     if(fruit1==columnName[i]){
         df1[f,i]<-count1
         ect...

但是,我正在处理一个相当大的数据集,而这似乎并不是正确的方法.

However, i'm dealing with a rather large dataset, and this doesn't seem like the right way to do it.

推荐答案

data.table选项:

A data.table option:

library(data.table)
setDT(df2)

# add row number
df2[, r := .I]

# "melt" common columns together
cols = c("fruit", "count")
m2 = melt(df2, measure=patterns(cols), value.name=cols)

# add unobserved fruits, if any
m2[, fruit := factor(fruit, levels = names(df1))]

# "cast" each fruit to its own column, ignoring NA rows
dcast(m2[!is.na(fruit)], r ~ fruit, fill = 0L, drop = FALSE)


   r apple orange banana pear grape
1: 1     2      0      0    1     0
2: 2     0      2      0    0     4
3: 3     0      0      1    0     0

factor 只是为了防止您在 df1 中有一些额外的级别而不会出现在 df2 中.如果一行中的 fruit1 fruit2 均为空白,则必须弄清楚如何扩展此方法.

The factor thing is just in case you have some extra levels in df1 that don't appear in df2. If you have a row where both fruit1 and fruit2 are blank, you'll have to figure out how you want to extend this approach.

这篇关于通过将值与列名匹配来填充 data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:03