我有一个仅具有列名df1
的df模板(0个观察值),我需要从df2
中获取值(整个列)以完成df1
。
df1
具有ID列名称,我需要用df2
中的SKU列来完成。但是df1
也有我需要保留的NA
列,例如:df1$ios_url
。
我尝试过:
df1$id <- df2$SKU
但是出现此错误:
`$ <-。data.frame`(`* tmp *`,id,value = c(“ 0005CR”,“ 0020MG”,“ 0030HG”,: 替换有7041行,数据有0
df1 :
df1 <- structure(list(id = character(0), title = character(0), ios_url = character(0),
ios_app_store_id = character(0), ios_app_name = character(0),
android_url = character(0), android_package = character(0),
android_app_name = character(0), windows_phone_url = character(0),
windows_phone_app_id = character(0), windows_phone_app_name = character(0),
description = character(0), google_product_category = character(0),
product_type = character(0), link = character(0), image_link = character(0),
condition = character(0), availability = character(0), price = character(0),
sale_price = character(0), sale_price_effective_date = character(0),
gtin = character(0), brand = character(0), mpn = character(0),
item_group_id = character(0), gender = character(0), age_group = character(0),
color = character(0), size = character(0), shipping = character(0),
custom_label_0 = character(0)), .Names = c("id", "title",
"ios_url", "ios_app_store_id", "ios_app_name", "android_url",
"android_package", "android_app_name", "windows_phone_url", "windows_phone_app_id",
"windows_phone_app_name", "description", "google_product_category",
"product_type", "link", "image_link", "condition", "availability",
"price", "sale_price", "sale_price_effective_date", "gtin", "brand",
"mpn", "item_group_id", "gender", "age_group", "color", "size",
"shipping", "custom_label_0"), row.names = integer(0), class = c("tbl_df",
"tbl", "data.frame"), spec = structure(list(cols = structure(list(
id = structure(list(), class = c("collector_character", "collector"
)), title = structure(list(), class = c("collector_character",
"collector")), ios_url = structure(list(), class = c("collector_character",
"collector")), ios_app_store_id = structure(list(), class = c("collector_character",
"collector")), ios_app_name = structure(list(), class = c("collector_character",
"collector")), android_url = structure(list(), class = c("collector_character",
"collector")), android_package = structure(list(), class = c("collector_character",
"collector")), android_app_name = structure(list(), class = c("collector_character",
"collector")), windows_phone_url = structure(list(), class = c("collector_character",
"collector")), windows_phone_app_id = structure(list(), class = c("collector_character",
"collector")), windows_phone_app_name = structure(list(), class = c("collector_character",
"collector")), description = structure(list(), class = c("collector_character",
"collector")), google_product_category = structure(list(), class = c("collector_character",
"collector")), product_type = structure(list(), class = c("collector_character",
"collector")), link = structure(list(), class = c("collector_character",
"collector")), image_link = structure(list(), class = c("collector_character",
"collector")), condition = structure(list(), class = c("collector_character",
"collector")), availability = structure(list(), class = c("collector_character",
"collector")), price = structure(list(), class = c("collector_character",
"collector")), sale_price = structure(list(), class = c("collector_character",
"collector")), sale_price_effective_date = structure(list(), class = c("collector_character",
"collector")), gtin = structure(list(), class = c("collector_character",
"collector")), brand = structure(list(), class = c("collector_character",
"collector")), mpn = structure(list(), class = c("collector_character",
"collector")), item_group_id = structure(list(), class = c("collector_character",
"collector")), gender = structure(list(), class = c("collector_character",
"collector")), age_group = structure(list(), class = c("collector_character",
"collector")), color = structure(list(), class = c("collector_character",
"collector")), size = structure(list(), class = c("collector_character",
"collector")), shipping = structure(list(), class = c("collector_character",
"collector")), custom_label_0 = structure(list(), class = c("collector_character",
"collector"))), .Names = c("id", "title", "ios_url", "ios_app_store_id",
"ios_app_name", "android_url", "android_package", "android_app_name",
"windows_phone_url", "windows_phone_app_id", "windows_phone_app_name",
"description", "google_product_category", "product_type", "link",
"image_link", "condition", "availability", "price", "sale_price",
"sale_price_effective_date", "gtin", "brand", "mpn", "item_group_id",
"gender", "age_group", "color", "size", "shipping", "custom_label_0"
)), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
DF2 :
df2 <- structure(list(TIENDA = c("CURACAO", "CURACAO", "CURACAO", "CURACAO",
"CURACAO", "CURACAO"), SKU = c("0005CR", "0020MG", "0030HG",
"0055CC", "006029T-053-000", "006544WM-053"), NOMBRE = c("Parrilla Mr Grill Camping Redonda 31cm",
"Parrilla Mr Grill Mini Grill + Maletín 31cm", "Parrilla Mr Grill Handy Grill 36cm",
"Parrilla Mr Grill Cilíndrica Con Tapa", "Olla Arrocera Oster 006029T-053-000 1.8L - Turquesa",
"Tostadora Oster 006544WM - Rojo"), PRECIO_OFERTA = c(39, 89,
69, 169, 178.99, 99), PRECIO_LISTA = c(49, 129, 89, 169, 179,
99), PRECIO_INDEXADO = c(39, 89, 69, 169, 178.9, 99), URLSEO = c("https://www.lacuracao.pe/curacao/parrilla-mr-grill-camping-redonda-31cm-0005cr",
"https://www.lacuracao.pe/curacao/parrilla-mr-grill-mini-grill--maletín-31cm-0020mg",
"https://www.lacuracao.pe/curacao/parrilla-mr-grill-handy-grill-36cm-0030hg",
"https://www.lacuracao.pe/curacao/parrilla-mr-grill-cilíndrica-con-tapa-0055cc",
"https://www.lacuracao.pe/curacao/olla-arrocera-oster-006029t-053-000-18l---turquesa--006029t-053-000",
"https://www.lacuracao.pe/curacao/tostadora-oster-006544wm---rojo--006544wm-053"
), CATEGORIA_VENTA = c("Aire Libre", "Aire Libre", "Aire Libre",
"Escápate de la Rutina", "Ollas Arroceras", "Oster"), CATEGORIA_MAESTRA = c("HEC0101MR",
"HEC0101MR", "HEC0101MR", "HED0101MR", "APA1301OS", "APA1701OS"
), PUBLICADO = c(1L, 1L, 1L, 1L, 0L, 0L), ADQUIRIBLE = c(1L,
1L, 1L, 1L, 1L, 1L), FULLIMAGE = c("../efe_cat_as/646x1000/0005CR_1B.jpg",
"../efe_cat_as/646x1000/0020MG_1.jpg", "../efe_cat_as/646x1000/0030HG_1B.jpg",
"../efe_cat_as/646x1000/0055CC_1.jpg", "../efe_cat_as/646x1000/006029T-053-000_1.jpg",
"../efe_cat_as/646x1000/006544WM-053_1.jpg"), THUMBNAIL = c("../efe_cat_as/200x310/0005CR_1B.jpg",
"../efe_cat_as/200x310/0020MG_1.jpg", "../efe_cat_as/200x310/0030HG_1B.jpg",
"../efe_cat_as/200x310/0055CC_1.jpg", "../efe_cat_as/200x310/006029T-053-000_1.jpg",
"../efe_cat_as/200x310/006544WM-053_1.jpg"), MARCA = c("MR GRILL",
"MR GRILL", "MR GRILL", "MR GRILL", "Oster", "Oster"), TAMAÑO = c("M",
"M", "M", "M", "XS", "XS"), LASTUPDATE = structure(c(1525860198.288,
1525860198.295, 1525860198.301, 1525860198.307, 1519742666.606,
1519742666.61), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
MARCA_VERDE = c(1L, 1L, 1L, 1L, NA, NA), DIAS_VERDE = c(4L,
4L, 4L, 4L, NA, NA), FIELD3 = c(NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_
), TIPO_VERDE = c("Y3", "Y3", "Y3", "Y3", NA, NA)), .Names = c("TIENDA",
"SKU", "NOMBRE", "PRECIO_OFERTA", "PRECIO_LISTA", "PRECIO_INDEXADO",
"URLSEO", "CATEGORIA_VENTA", "CATEGORIA_MAESTRA", "PUBLICADO",
"ADQUIRIBLE", "FULLIMAGE", "THUMBNAIL", "MARCA", "TAMAÑO", "LASTUPDATE",
"MARCA_VERDE", "DIAS_VERDE", "FIELD3", "TIPO_VERDE"), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
答案 0 :(得分:1)
该错误指出df1
应该具有与df2
一样多的行。
这是在第一步中提取df1
的列名的一种方法。然后覆盖df1
,使其仅包含单个列id
。此列的观察结果与df2
相同。现在,我们可以从初始df1
创建所有列,并用NA
填充它们。
cols <- names(df1)
df1 <- data.frame(id = df2$SKU)
df1 <- `[<-`(df1, setdiff(cols, "id"), value = NA)
df1[, 1:4] # only printed the first four columns for illustration
# id title ios_url ios_app_store_id
#1 0005CR NA NA NA
#2 0020MG NA NA NA
#3 0030HG NA NA NA
#4 0055CC NA NA NA
#5 006029T-053-000 NA NA NA
#6 006544WM-053 NA NA NA
现在我们也可以替换其他列
df1$title <- df2$NOMBRE