วิธีจัดการกับคอลัมน์ที่มีค่าคั่นด้วยเครื่องหมายจุลภาคหลายค่าในภาษา R

มีคอลัมน์หนึ่งในชุดข้อมูลของฉัน (ไฟล์ CSV) ได้แก่ สิ่งอำนวยความสะดวก โดยมีค่าต่างๆ เช่น{"อินเทอร์เน็ตไร้สาย", "เข้าถึงได้สำหรับเก้าอี้รถเข็น", ห้องครัว, ลิฟต์, "กริ่ง/อินเตอร์คอมไร้สาย", เครื่องทำความร้อน, เครื่องซักผ้า, เครื่องอบผ้า, สิ่งจำเป็น, แชมพู, ไม้แขวนเสื้อ, "พื้นที่ทำงานที่เป็นมิตรต่อแล็ปท็อป"}, {ทีวี, "เคเบิล ทีวี", อินเทอร์เน็ต, "อินเทอร์เน็ตไร้สาย", "เครื่องปรับอากาศ", ห้องครัว, "อนุญาตให้สูบบุหรี่ได้", "อนุญาตให้นำสัตว์เลี้ยงเข้าได้", "กริ่ง/อินเตอร์คอมไร้สาย", เครื่องทำความร้อน, "เหมาะสำหรับครอบครัว/เด็ก", "เครื่องตรวจจับควัน", "คาร์บอน" monoxide เป็นต้น มีประมาณ 10,000 coloumn แบบนี้ ฉันต้องการแปลงสิ่งอำนวยความสะดวกแต่ละรายการให้เป็นคอลัมน์ใหม่และต้องการสร้างค่าเป็น 0 หรือ 1 สำหรับแต่ละรายการ ตัวอย่างเช่น

ควรสร้าง Coloumns ดังต่อไปนี้ อินเตอร์เน็ตไร้สาย รถเข็นคนพิการเข้าได้ ลิฟต์ในครัว ออด/ไร้สาย 1 0 1 1 0

โดยพื้นฐานแล้ว แต่ละองค์ประกอบของคอลัมน์จะสร้างคอลัมน์ใหม่และค่าของมันควรเป็น 0 และ 1 ขึ้นอยู่กับว่าสิ่งอำนวยความสะดวกนั้นมีอยู่ในคอลัมน์หรือไม่

ฉันมีอินพุตดังนี้: ป้อนคำอธิบายรูปภาพที่นี่

และฉันต้องการผลลัพธ์ดังนี้:

ป้อนคำอธิบายรูปภาพที่นี่


person Rachit Jain    schedule 18.04.2020    source แหล่งที่มา
comment
สิ่งนี้อาจช่วยให้คุณเริ่มต้นได้: สมมติว่า daatframe ของคุณชื่อ dfคุณสามารถทำได้: amenities_clean <- gsub('[{}"]', '', df$amenities) # remove unwanted stuff amenities_unique <- unique(unlist(strsplit(amenities_clean, ","))) # get a list of unique amenities df[amenities_unique] <- NA # set up the columns for each amenity   -  person Chris Ruehlemann    schedule 19.04.2020
comment
@ChrisRuehlemann: ขอบคุณสำหรับรหัสแยกด้านบน มันได้ผล ตอนนี้เป้าหมายของฉันคือการสร้างคอลัมน์ใหม่ใน df ด้วยค่าเหล่านี้ และหากค่านั้นมีอยู่ในคอลัมน์สิ่งอำนวยความสะดวกดั้งเดิม ให้ทำเครื่องหมายเป็น 1 อย่างอื่น 0   -  person Rachit Jain    schedule 26.04.2020


คำตอบ (1)


นี่เป็นวิธีแก้ปัญหา (ค่อนข้างซับซ้อน) (สำหรับปัญหาที่ค่อนข้างซับซ้อน):

ข้อมูล:

df <- data.frame(
  id = 1:2,
  amenities = c('{"Wireless Internet","Wheelchair accessible",Kitchen,Elevator,"Buzzer/wireless intercom",Heating,Washer,Dryer,Essentials,Shampoo,Hangers,"Laptop friendly workspace"}',
                 '{TV,"Cable TV",Internet,"Wireless Internet","Air conditioning",Kitchen,"Smoking allowed","Pets allowed","Buzzer/wireless intercom",Heating,"Family/kid friendly","Smoke detector","Carbon monoxide}'))

เตรียมข้อมูล:

amenities_clean <- gsub('[{}"]', '', df$amenities) # remove unwanted stuff 
amenities_split <- strsplit(amenities_clean, ",") # split rows into individual amenities
amenities_unique <- unique(unlist(strsplit(amenities_clean, ","))) # get a list of unique amenities 
df[amenities_unique] <- NA # set up the columns for each amenity

ตอนนี้สำหรับการวิเคราะห์เนื้อโดยใช้ str_detect จากแพ็คเกจ stringr:

# record presence/absence of individual amenities in each new column:

library(stringr)
for(i in 1:ncol(df[amenities_unique])){
  for(j in 1:nrow(df)){
    df[amenities_unique][j,i] <- 
      ifelse(str_detect(amenities_split[j], names(df[amenities_unique][i])), 1, 0)
  }
}

สิ่งนี้จะแจ้งคำเตือน แต่ดูเหมือนว่าจะไม่มีนัยสำคัญเนื่องจากผลลัพธ์ถูกต้อง:

df
  id
1  1
2  2
                                                                                                                                                                                            amenities
1                               {"Wireless Internet","Wheelchair accessible",Kitchen,Elevator,"Buzzer/wireless intercom",Heating,Washer,Dryer,Essentials,Shampoo,Hangers,"Laptop friendly workspace"}
2 {TV,"Cable TV",Internet,"Wireless Internet","Air conditioning",Kitchen,"Smoking allowed","Pets allowed","Buzzer/wireless intercom",Heating,"Family/kid friendly","Smoke detector","Carbon monoxide}
  Wireless Internet Wheelchair accessible Kitchen Elevator Buzzer/wireless intercom Heating Washer Dryer
1                 1                     1       1        1                        1       1      1     1
2                 1                     0       1        0                        1       1      0     0
  Essentials Shampoo Hangers Laptop friendly workspace TV Cable TV Internet Air conditioning Smoking allowed
1          1       1       1                         1  0        0        1                0               0
2          0       0       0                         0  1        1        1                1               1
  Pets allowed Family/kid friendly Smoke detector Carbon monoxide
1            0                   0              0               0
2            1                   1              1               1

แก้ไข:

อีกทางหนึ่งและอาจประหยัดกว่า แทนที่จะเป็น forloop ที่ซ้อนกัน คุณสามารถใช้ฟังก์ชัน apply เช่นนี้ (ขึ้นอยู่กับเวกเตอร์ amenities_split และ amenities_unique จากขั้นตอนการเตรียมการของโซลูชันแรก):

cbind(df, t(sapply(amenities_split, function(x) 
  table(factor(x, levels = amenities_unique)))))
person Chris Ruehlemann    schedule 26.04.2020