Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.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/qt/6.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_Dplyr_Plyr_Gsub - Fatal编程技术网

R 复制数据帧并替换值

R 复制数据帧并替换值,r,dataframe,dplyr,plyr,gsub,R,Dataframe,Dplyr,Plyr,Gsub,如果我有两个数据帧: Df1: Name1 Name2 Destination1 A I London B J Paris C K New York D L Bangkok E M Singapore Df2: Theme Pattern Luxury luxury hotels in {d} City city hotels {d} Br

如果我有两个数据帧:

Df1:
Name1 Name2 Destination1
  A     I       London
  B     J       Paris
  C     K       New York
  D     L       Bangkok
  E     M       Singapore

Df2:
Theme      Pattern
Luxury      luxury hotels in {d} 
City        city hotels {d}
Break        breaks in {d} 
Package      {d} packages
本质上,我想要一个新的数据帧,其中对于Df1中的每个destination1,我拥有Df2中的每个模式,同时保留主题列和Df1中的名称1和名称2列

例如,期望输出:

Df3:
Name 1      Name 2     Destination 1  Theme     Pattern
A            I            London      Luxury     luxury hotels in {London} 
A            I            London      City       city hotels {London}
A            I            London      Break       breaks in {London} 
A            I            London      Packages    {London} packages
B            J            Paris       Luxury       luxury hotels in {Paris} 
B            J            Paris       City         city hotels {Paris}
B            J            Paris       Break        breaks in {Paris} 
B            J            Paris       Packages     {Paris} packages
C etc....

您可以为每个数据集创建一个新变量,然后在联接后将其删除。你可以在下面做

library(dplyr)
Df1$new <- "lol"

Df2$new <- "lol"

Df3 <- full_join(Df1,Df2) %>% select(-new)


**example:
df1 <- data.frame(a=c(1:5),b=c(7:11))

df2 <- data.frame(c=c(12:16),d=c(17:21))

df1$new <- "lol"
df2$new <- "lol"
library(dplyr)

full_join(df1,df2) %>% select(-new)**
库(dplyr)
Df1$new不是完全相同的数据(您应该提供生成数据的代码),但是这可以完成您正在寻找的事情!虽然不是很优雅,但我必须承认

A=data.frame(c1=c("A", "B", "C"), c2=c("london", "paris", "berlin"))
B=data.frame(c3=c("a", "b", "c"), c4=c("la{d}", "{d}lala", "lala{d}la"))
# aggregate the df
AB <- data.frame(c1=rep(A$c1, nrow(B)), c2=rep(A$c2, nrow(B)), 
                 c3=rep(B$c3, each=nrow(A)), c4=rep(B$c4, each=nrow(A)))
# change {d} in city names
AB$c4 <- sapply(1:nrow(AB), function(x) gsub("\\{d\\}", 
                                        paste(" ", AB[x,"c2"], " "), AB[x,"c4"])) 
# regroup by city names
AB <- AB[order(AB$c2),] 
AB # enjoy
A=data.frame(c1=c(“A”、“B”、“c”),c2=c(“伦敦”、“巴黎”、“柏林”))
B=data.frame(c3=c(“a”,“B”,“c”),c4=c(“la{d}”,“lala{d}”,“lala{d}”))
#聚合df

AB您可以使用dplyr和tidyr解决方案来实现这一点:首先,将Df2重塑为宽格式,并使用Df1进行cbind;然后聚集到原始长格式。然后使用带有正则表达式的gsub将{d}替换为目标

library(dplyr)
library(tidyr)

Df1 <- data.frame(name1 = LETTERS[1:5],
                  name2 = LETTERS[9:13],
                  Destination1 = c("London", "Paris", "New York", "Bangkok", "Singapore")
                  )

Df2 <- data.frame(Theme = c("Luxury", "City", "Break", "Package"),
                  Pattern = c("Luxury hotels in {d}",
                          "City hotels in {d}",
                          "Breaks in {d}",
                          "{d} packages")
                 )

Df3 <- Df1 %>% 
  # reshape Df2 to wide format and combine it with Df1
  cbind(spread(data = Df2, key = Theme, value = Pattern)) %>%
  # convert back to long format
  gather(key = Theme, value = Pattern, Break:Package) %>%
  # replace {d} with Destination
  mutate(Pattern = gsub(pattern = "\\{d\\}",
                        replacement = Destination1,
                        x = Pattern))
库(dplyr)
图书馆(tidyr)
Df1%
#将{d}替换为目标
突变(Pattern=gsub(Pattern=“\\{d\\}”,
替换=目的地1,
x=模式)