Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何创建一个数据框,其中字段是另一个数据框中的值_R_Dataframe - Fatal编程技术网

R 如何创建一个数据框,其中字段是另一个数据框中的值

R 如何创建一个数据框,其中字段是另一个数据框中的值,r,dataframe,R,Dataframe,我是R-programming的新手,非常感谢本论坛提供的宝贵建议 我正在以下面的格式处理数据帧DF1 我正在尝试创建另一个数据帧DF2,其中DF1的“Description”列的值必须是DF2中的列名,并且必须相应地捕获与描述相对应的贷方和借方值(贷方为正数,借方为负数) 如果您能提出在R实现这一目标的逻辑,我将不胜感激。提前感谢 dput(DF1) is as below structure(list(Account_number = c(1234, 1234, 1234, 3456,

我是R-programming的新手,非常感谢本论坛提供的宝贵建议

我正在以下面的格式处理数据帧DF1

我正在尝试创建另一个数据帧DF2,其中DF1的“Description”列的值必须是DF2中的列名,并且必须相应地捕获与描述相对应的贷方和借方值(贷方为正数,借方为负数)

如果您能提出在R实现这一目标的逻辑,我将不胜感激。提前感谢

dput(DF1) is as below
structure(list(Account_number = c(1234, 1234, 1234, 3456, 3456, 
4567, 4567), Credit = c(5.1, NA, 10, NA, 10, 5, NA), Debit = c(NA, 
7.2, NA, 20, NA, NA, 30), Description = c("abc", "pqr", "xyz", 
"xyz", "abc", "pqr", "abc")), .Names = c("Account_number", "Credit", 
"Debit", "Description"), row.names = c(NA, -7L), class = c("tbl_df", 
"tbl", "data.frame"))
面容整洁:

DF1 %>% mutate(Debit=-Debit) %>% 
  gather(k,v,-Account_number,-Description) %>%
  select(-k) %>% filter(!is.na(v)) %>% 
  spread(Description,v)

# A tibble: 3 x 4 
#  Account_number    abc    pqr   xyz
#*          <dbl>  <dbl>  <dbl> <dbl>
#1          1234.   5.10  -7.20   10.
#2          3456.  10.0   NA     -20.
#3          4567. -30.0    5.00   NA
DF1%>%变异(借方=-借方)%>%
聚集(k,v,-科目编号,-描述)%>%
选择(-k)%%>%过滤器(!is.na(v))%%>%
排列(描述,v)
#一个tibble:3x4
#账号abc pqr xyz
#*               
#1          1234.   5.10  -7.20   10.
#2          3456.  10.0 NA-20。
#3          4567. -30.0 5.00北美

能否以可复制粘贴的形式提供
DF1
?在你的问题的末尾贴上
dput(DF1)
的输出。嗨,马库斯-我已经把dput结果添加到我的问题中了。再次感谢你的帮助。Cheers更改$Debit符号后,解决方案的其余部分由Hi Nicolas负责。谢谢你的解决方案。尝试运行它,但失败,因为尚未定义使用的对象k和v。在运行上述解决方案之前,如果我需要定义对象k和v,您能告诉我吗?嗨,Nicolas,请忽略我之前的评论。这是我的一个错误。你建议的解决方案很有效。谢谢