简体   繁体   English

R:有条件地将数据从列复制到行

[英]R: Conditionally duplicating data from columns into rows

I am trying to insert new rows that would duplicate some data contained in the row but the first column would be unique data that is inserted from an existing column within R.我正在尝试插入新行来复制该行中包含的某些数据,但第一列将是从 R 中的现有列插入的唯一数据。

I am trying to set this data up to be utilized in Tableau and create a network visualization.我正在尝试将此数据设置为在 Tableau 中使用并创建网络可视化。 I don't want my customers entering the data to insert a lot of duplicate data in order to create this visualization.我不希望我的客户在输入数据时插入大量重复数据以创建此可视化。

My current data looks like this:我当前的数据如下所示:

            Connection.ID     From             To              Note
  1         1                 Niamh MacCallum  James Fraser    Niamh and James are coworkers
  2         2                 James Fraser     Simon David     James and Simon are brothers
  3         3                 Niamh MacCallum  Tom Ashton      Niamh recruited Tom to join her organization

This is some fake data I created that replicates my company's data, but the goal is being able to visualize connections between our employees and customers/volunteers they meet and form professional relationships with.这是我创建的一些虚假数据,用于复制我公司的数据,但目标是能够将我们的员工与他们遇到的客户/志愿者之间的联系可视化并与之建立专业关系。

I would like my data to look like this which I export into a csv:我希望我的数据看起来像我导出到 csv 的样子:

            Connection.ID     Node.Name        Notes
  1         1                 Niamh MacCallum  Niamh and James are coworkers
  2         1                 James Fraser     Niamh and James are coworkers
  3         2                 James Fraser     James and Simon are brothers
  4         2                 Simon David      James and Simon are brothers     
  5         3                 Niamh MacCallum  Niamh recruited Tom to join her organization
  6         3                 Tom Ashton       Niamh recruited Tom to join her organization

I've found a couple of resources that create something similar, the best one being this previously-asked question, but it wasn't quite getting to what I needed or I honestly could have been misapplying it ( conditionally duplicating rows in a data frame ).我发现了一些创建类似内容的资源,最好的一个是之前提出的问题,但它并没有完全满足我的需要,或者老实说我可能会误用它( 有条件地复制数据框中的行)。 I thought I could create the same thing while removing the "To" column and renaming "From" to "Node.Name" but I created repetitive data that inserted six copies of each row while also misapplying notes to the wrong connections.我以为我可以在删除“To”列并将“From”重命名为“Node.Name”的同时创建相同的内容,但我创建了重复数据,这些数据插入了每行的六个副本,同时还将注释误用于错误的连接。

I'd appreciate any help!我很感激任何帮助! I'm fairly new to R and self-taught, so if you have a solution or a resource where I can learn the solution that'd be great too.我是 R 的新手并且是自学的,所以如果你有一个解决方案或资源,我可以学习这个解决方案也很棒。 Thanks!谢谢!

EDIT: Found a similar question I had not seen before, so I am adding it here in case someone else finds this and can reference both of them: Create network files from "classic" dataframe in R - igraph编辑:发现了一个我以前从未见过的类似问题,所以我在这里添加它以防其他人发现这个问题并且可以引用它们: Create network files from "classic" dataframe in R - igraph

This is a wide to long transformation that can be done with melt from the reshape2 package.这是一个从宽到长的转换,可以通过reshape2包中的melt来完成。 Do:做:

df2 = melt(data = df, 
           id.vars = c("Connection.ID","Note"), 
           measure.vars = c("From","To"), 
           variable.name = 'From_To',
           value.name = "Node.Name" )

# Remove the unwanted From_To column
df2$From_To = NULL

Result:结果:

> df2
  Connection.ID                                             Note       Node.Name
1             1                    Niamh and James are coworkers Niamh MacCallum
2             2                     James and Simon are brothers    James Fraser
3             3     Niamh recruited Tom to join her organization Niamh MacCallum
4             1                    Niamh and James are coworkers    James Fraser
5             2                     James and Simon are brothers     Simon David
6             3     Niamh recruited Tom to join her organization      Tom Ashton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM