-1

After my last question, I have data frame like this:

data_pangan

And I also have a data like this:

my_data

Data that I need from "my_data":

Data that I need

How to copy "data_pangan" attributes to "my_data" if that attributes not exist in "my_data"? And how to set the value to "no"?

camille
  • 16,432
  • 18
  • 38
  • 60
  • [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. What exactly is the output you want? – camille Dec 21 '19 at 22:40

1 Answers1

0

We can use setdiff to find the column names that are not in the 'my_data' and assign those to 'no'

my_data[setdiff(names(data_pangan)[-1], names(my_data[-1]))] <- "no"
my_data
#  id_kategori Ekstruksi Permisahan Pendinginan Pemotogan Pengeringan
#1           1        no        yes          no        no          no

data

data_pangan <- structure(list(id_kategori = 1:3, Ekstruksi = c("yes", "no", 
"yes"), Permisahan = c("no", "yes", "no"), Pendinginan = c("yes", 
"yes", "no"), Pemotogan = c("yes", "no", "no"), Pengeringan = c("no", 
"no", "yes")), class = "data.frame", row.names = c(NA, -3L))

my_data <- structure(list(id_kategori = 1, Ekstruksi = "no", Permisahan = "yes"),
class = "data.frame", row.names = c(NA, 
-1L))
akrun
  • 874,273
  • 37
  • 540
  • 662