查看示例数据:
> dput(data)
structure(list(mpg = c("Mazda RX4", "Mazda RX4 Wag", "Datsun 710",
"Hornet 4 Drive", "Hornet Sportabout", "Valiant", "Duster 360",
"Merc 240D", "Merc 230", "Merc 280", "Merc 280C", "Merc 450SE",
"Merc 450SL", "Merc 450SLC", "Cadillac Fleetwood", "Lincoln Continental",
"Chrysler Imperial", "Fiat 128", "Honda Civic", "Toyota Corolla",
"Toyota Corona", "Dodge Challenger", "AMC Javelin", "Camaro Z28",
"Pontiac Firebird", "Fiat X1-9", "Porsche 914-2", "Lotus Europa",
"Ford Pantera L", "Ferrari Dino", "Maserati Bora", "Volvo 142E"
), cyl = c(6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8,
4, 4, 4, 4, 8, 8, 8, 8, 4, 4, 4, 8, 6, 8, 4)), .Names = c("mpg",
"cyl"), row.names = c(NA, -32L), class = "data.frame")
假设第二列是簇号。请记住,在我的原始数据而不是第二列中的数字我有字符串。如果它们相同则意味着它们属于同一个集群。我想从这些集群中创建列,并将属于此集群的car
放入每一行。当然,列的名称应与群集名称相同。
答案 0 :(得分:2)
我们可以使用table
,转换为data.frame
,循环遍历列并使用ifelse
将1值替换为相应的“mpg”,将0替换为NA。
d1 <- as.data.frame.matrix(table(1:nrow(data),data$cyl))
d1[] <- lapply(d1, function(x) ifelse(x!=0, as.character(data$mpg), NA))
如果我们只对1个元素感兴趣,而不是'data.frame',请将其保留在list
中,因为长度会有所不同。
lapply(d1, function(x) {as.character(data$mpg)[x!=0]})