我很抱歉,因为我不太确定如何在不使问题变得冗长的情况下说出我的问题,因为重复的行还需要与原始行有一些更改的值。
我有两个数据框。第一个df1记录了实际从源到目的地的所有路径,而第二个df2包含了所有可能的路径。一些样本数据如下:
行 | 资源 | 目的地 | 有效载荷 |
---|---|---|---|
1个 | 一种 | 乙 | 10010101 |
2 | 一种 | d | 11101011 |
3 | 一种 | 乙 | 10111111 |
4 | Ë | 乙 | 01100110 |
行 | 资源 | 目的地 |
---|---|---|
1个 | 一种 | 乙 |
2 | 乙 | 一种 |
3 | 乙 | C |
4 | 乙 | Ë |
5 | 乙 | F |
6 | 一种 | d |
7 | d | 一种 |
8 | d | C |
9 | d | H |
对于我的数据,假设如果某个对象采用路径A-> B,则它也会采用源自B而不是原始来源的所有可能路径(请考虑网络集线器。彼此分开)。因此,由于我们有一个从A-> B的有效负载,因此我还需要记录从B到C,E和F的相同有效负载。我目前正在下面的FOR循环中完成此操作,但我想知道是否有更好的方法,最好是不使用循环的方法。我对R也有些陌生,因此即使对我的代码进行简单的更正也应受到赞赏。
for (row in 1:dim(df1)[1]){
initialSource <- df1$source[row] #saves the initial source
paths <- df1[row,] #saves the current row for duplication
paths <- paths[rep(1, times = count(df2[df2$source %in% df1$destination[row], ])[[1]]), ] #duplicates the row
paths$source <- paths$destination #replaces the source values to be the location of the hub
paths$destination <- df2$destination[df2$source %in% paths$destination] #replaces the destination values to be every connection from the hub
paths <- paths[!(paths$destination %in% initialSource), ] #removes the row that would indicate data being sent back to the source
masterdf <- rbind(masterdf, paths) #saving the new data to a larger data frame that df1 is actually a sample of.
}
在paths
具有以上数据的第一个循环结束时的数据帧如下所示:
行 | 资源 | 目的地 | 有效载荷 |
---|---|---|---|
1个 | 乙 | C | 10010101 |
2 | 乙 | Ë | 10010101 |
3 | 乙 | F | 10010101 |
也许您可以尝试合并两个数据框。使用base R,merge
您可以执行以下操作(使用来自的“目标”df1
和来自的“源” df2
)。您将需要删除行以排除所描述的“原始来源”。重命名和选择列将为您提供最终输出。如果这是您的想法,请告诉我。
d <- subset(
merge(df1, df2, by.x = "Destination", by.y = "Source", all = TRUE),
Source != Destination.y
)
data.frame(
Source = d$Destination,
Destination = d$Destination.y,
Payload = d$Payload
)
输出量
Source Destination Payload
1 B C 10010101
2 B E 10010101
3 B F 10010101
4 B C 10111111
5 B E 10111111
6 B F 10111111
7 B C 1100110
8 B F 1100110
9 B A 1100110
10 D C 11101011
11 D H 11101011
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句