2016-12-12 5 views
-2

私はcounts、Nとパーセンテージで要約data.frameを作成しています。 要約data.frameには、カテゴリ、n、カウント、パーセンテージの名前があります。 count.data.frameには、名前 - >カテゴリ、カウントがあります。 カテゴリは文字です。多くの場合、カウントデータフレームの行は、サマリーの行よりも小さくなります。私はMSSQLでこれを行うだろう要約data.frameフィールドをRの別のdata.frameの値で更新するにはどうすればよいですか?

方法は

これはRで行うことができる方法
update summary 
set summary.count = counts.count 
from summary 
inner join counts 
on summary.category = counts.category 

のですか?

サポートコード。

summary <- data.frame(category=c("apples","oranges","pears"),N=10,count=0,percentage=0) 
> summary 
    category N count percentage 
1 apples 10  0   0 
2 oranges 10  0   0 
3 pears 10  0   0 
> counts <- data.frame(category=c("apples","pears"), count = c(5,5)) 
> counts 
    category count 
1 apples  5 
2 pears  5 

# desired outcome after processing 
> summary 
    category N count percentage 
1 apples 10  5   0.5 
2 oranges 10  0   0 
3 pears 10  5   0.5 
+1

が ''パッケージdplyr'からinner_join'を試してみてください参照して結合します。 – Gopala

+0

ありがとう...私は、R data.frame共用体を検索してマージコマンドを発見しました。 –

+1

ええ、それも動作します。私はちょうど 'dplyr'に慣れています。 :) – Gopala

答えて

0

R data.frame unionを検索した後にmergeコマンドが見つかりました。 SQLにマップをマージする方法についての概要を表示するには

> summary <- data.frame(category=c("apples","oranges","pears"),N=10) 
> summary 
    category N 
1 apples 10 
2 oranges 10 
3 pears 10 
> counts <- data.frame(category=c("apples","pears"), count = c(5,5)) 
> counts 
    category count 
1 apples  5 
2 pears  5 
> merged <- merge(summary, counts, by="category", all.x = TRUE) 
> merged 
    category N count 
1 apples 10  5 
2 oranges 10 NA 
3 pears 10  5 
> merged[is.na(merged)] <- 0 
> merged 
    category N count 
1 apples 10  5 
2 oranges 10  0 
3 pears 10  5 
> merged["percentage"] = merged$count/merged$N 
> merged 
    category N count percentage 
1 apples 10  5  0.5 
2 oranges 10  0  0.0 
3 pears 10  5  0.5 

How to join (merge) data frames (inner, outer, left, right)?

関連する問題