我有这个df
:
df = data.frame(Meaning = c('Tax', 'Internet', 'Tax', 'Phone', 'Tax', 'Car'),
Code = c(4656, 6152, 4656, 6150, 4656, 6151),
Total = c(0.73, 4.4, 1.33, 8, 1.67, 10),
Tax = c(0.73, NA, 1.33, NA, 1.67, NA),
Subtotal = c(NA, 3.67, NA, 6.67, NA, 8.33),
stringsAsFactors = FALSE)
> df
Meaning Code Total Tax Subtotal
Tax 4656 0.73 0.73 NA
Internet 6152 4.40 NA 3.67
Tax 4656 1.33 1.33 NA
Phone 6150 8.00 NA 6.67
Tax 4656 1.67 1.67 NA
Car 6151 10.00 NA 8.33
我想使用reshape()
或stack
来获得另一个data.frame
,如下所示:
Code Meaning Category Price
6152 Internet Total 4.4
6152 Internet Subtotal 3.67
4656 Tax Subtotal 0.73
6150 Phone Total 8
6150 Phone Subtotal 6.67
4656 Tax Subtotal 1.33
6151 Car Total 10
6151 Car Subtotal 8.33
4656 Tax Subtotal 1.67
在Category
显示df
(Total
或Subtotal
)中的列的情况下,Price
的显示方式如下:Total
, Subtotal
,Tax
显示在df
上。
到目前为止,我尝试了:
cbind(df[1:2], stack(lapply(df[-c(1:2)], as.character)))
但是它会检索:
Meaning Code values ind
Tax 4656 0.73 Total
Internet 6152 4.4 Total
Tax 4656 1.33 Total
Phone 6150 8 Total
Tax 4656 1.67 Total
Car 6151 10 Total
Tax 4656 0.73 Tax
Internet 6152 <NA> Tax
Tax 4656 1.33 Tax
Phone 6150 <NA> Tax
Tax 4656 1.67 Tax
Car 6151 <NA> Tax
Tax 4656 <NA> Subtotal
Internet 6152 3.67 Subtotal
Tax 4656 <NA> Subtotal
Phone 6150 6.67 Subtotal
Tax 4656 <NA> Subtotal
Car 6151 8.33 Subtotal
有什么想法吗?
注意:我已经尝试了所有这些答案,但是由于我的df
有一些NA
,因此该解决方案无法正常工作。
Answer 1,Answer 2,Answer 3
答案 0 :(得分:0)
这看起来正确吗?
library(tidyverse)
df %>%
gather(Total, Tax, Subtotal, key="key", value="value") %>%
arrange(Code)
Meaning Code key value
1 Tax 4656 Total 0.73
2 Tax 4656 Total 1.33
3 Tax 4656 Total 1.67
4 Tax 4656 Tax 0.73
5 Tax 4656 Tax 1.33
6 Tax 4656 Tax 1.67
7 Tax 4656 Subtotal NA
8 Tax 4656 Subtotal NA
9 Tax 4656 Subtotal NA
10 Phone 6150 Total 8.00
11 Phone 6150 Tax NA
12 Phone 6150 Subtotal 6.67
13 Car 6151 Total 10.00
14 Car 6151 Tax NA
15 Car 6151 Subtotal 8.33
16 Internet 6152 Total 4.40
17 Internet 6152 Tax NA
18 Internet 6152 Subtotal 3.67
答案 1 :(得分:0)
我更喜欢使用melt
中的data.table
函数:
library(data.table)
melt(df,
id.vars = c('Meaning', 'Code'),
variable.name = 'Category',
value.name = 'Price')
Meaning Code Category price
1 Tax 4656 Total 0.73
2 Internet 6152 Total 4.40
3 Tax 4656 Total 1.33
4 Phone 6150 Total 8.00
5 Tax 4656 Total 1.67
6 Car 6151 Total 10.00
7 Tax 4656 Tax 0.73
8 Internet 6152 Tax NA
9 Tax 4656 Tax 1.33
10 Phone 6150 Tax NA
11 Tax 4656 Tax 1.67
12 Car 6151 Tax NA
13 Tax 4656 Subtotal NA
14 Internet 6152 Subtotal 3.67
15 Tax 4656 Subtotal NA
16 Phone 6150 Subtotal 6.67
17 Tax 4656 Subtotal NA
18 Car 6151 Subtotal 8.33