Есть ли способ добавить значения столбцов из 2 разных фреймов данных - PullRequest
0 голосов
/ 10 июля 2019

У меня есть два фрейма данных
df1:

DAT1 DAT3     DAT4    ...
 1   this is  this is
 2   this is  this is
 3   this is  this is

df2:

DAT1 DAT3       DAT4      ... 
 1   a comment  a comment
 2   a comment  a comment
 3   a comment  a comment

Я хочу найти способ добавить вторые столбцы данных (я знаю имя и положение столбцов, которые мне нужно добавить) к первому и получить обновленную версию первого, который имеет:
DF3:

DAT1 DAT3               DAT4               ... 
 1   this is a comment  this is a comment  
 2   this is a comment  this is a comment
 3   this is a comment  this is a comment

Дело в том, что реальные кадры данных имеют много строк и столбцов, поэтому цикл for() будет действительно неэффективным.

Ответы [ 4 ]

2 голосов
/ 10 июля 2019

Мы можем использовать base R без зацикливания

cols <- c("DAT3", "DAT4")     
df3 <- df1
df3[cols] <-matrix(paste(as.matrix(df1[-1]), as.matrix(df2[-1])), nrow = nrow(df1))
df3
#  DAT1              DAT3              DAT4
#1    1 this is a comment this is a comment
#2    2 this is a comment this is a comment
#3    3 this is a comment this is a comment

данные

df1 <- structure(list(DAT1 = 1:3, DAT3 = c("this is", "this is", "this is"
), DAT4 = c("this is", "this is", "this is")), class = "data.frame",
row.names = c(NA, 
-3L))

df2 <- structure(list(DAT1 = 1:3, DAT3 = c("a comment", "a comment", 
"a comment"), DAT4 = c("a comment", "a comment", "a comment")),
   class = "data.frame", row.names = c(NA, 
-3L))
2 голосов
/ 10 июля 2019

Мы можем использовать Map

cols <- c("DAT3", "DAT4")
df3 <- df1
df3[cols] <- Map(paste, df1[cols], df2[cols])

df3
#  DAT1              DAT3              DAT4
#1    1 this is a comment this is a comment
#2    2 this is a comment this is a comment
#3    3 this is a comment this is a comment
1 голос
/ 10 июля 2019

Если ваши данные упорядочены, я бы сделал что-то вроде этого:

#initiate the data.frame with the id
df3 <- data.frame(DAT1 = df1$DAT1)

#then run a for-loop with the names you know you need to concatenate
for (i in c('DAT3', 'DAT4')) {
  df3[[i]] <- paste(df1[[i]], df2[[i]])
}

Цикл for выполняет итерации только по именам.Ядро кода - paste, которое векторизовано и быстро.Таким образом, вы не столкнетесь с проблемами скорости

df3
#  DAT1              DAT3              DAT4
#1    1 this-is a-comment this-is a-comment
#2    2 this-is a-comment this-is a-comment
#3    3 this-is a-comment this-is a-comment
0 голосов
/ 10 июля 2019

a dplyr версия

df1 %>% inner_join(df2, by = "DAT1") %>% rowwise() %>%
  mutate(DAT3 = paste(DAT3.x, DAT3.y, collapse = " "),
         DAT4 = paste(DAT4.x, DAT4.y, collapse = " ")) %>%
  select(everything(), -contains("."))

OutPut

# A tibble: 3 x 3
   DAT1 DAT3              DAT4             
  <dbl> <chr>             <chr>            
1     1 this is a comment this is a comment
2     2 this is a comment this is a comment
3     3 this is a comment this is a comment
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...