[英]Splitting a column in R with ifelse condition
I have a column called Rooms
which has the following data:我有一个名为
Rooms
的列,其中包含以下数据:
'4+1' '3+1' '3' '2' '3+2' '4' '1' '2+1' '2+2' '5+1' '1+1' '6+1' '7+1' '5' '6' '0' '4+2' '8'
I wanted to split it to two columns where the delimiter is +
and I did this by doing thee following:我想将它拆分为分隔符为
+
两列,我通过执行以下操作来做到这一点:
splitingRoomsDF <- data.frame(do.call('rbind', strsplit(as.character(out$Rooms),'+',fixed=TRUE)),stringsAsFactors = FALSE)
Then in order to combine this dataframe with the main data I did:然后为了将这个数据框与主要数据结合起来,我做了:
data_final = cbind(out,splitingRoomsDF)
and when I printed it out I got this:当我打印出来时,我得到了这个:
The problem with the above output is that if we have a cell value in Rooms
that doesn't have the +
delimiter it replace both X1
and X2
with that value and I want the value of X2
to be zero if Rooms
is without +
delimiter.上面输出的问题是,如果我们在
Rooms
中有一个没有+
分隔符的单元格值,它会用该值替换X1
和X2
,如果Rooms
没有+
分隔符,我希望X2
的值为零。 I'm not sure using if-else is the right thing to do here but going with that for now.我不确定在这里使用 if-else 是否正确,但现在就使用它。
So what should I change in my code lines above in order to accurately make this change.那么我应该在上面的代码行中更改什么才能准确地进行此更改。
I've using this dataset: https://www.kaggle.com/dragonduck/property-listings-in-kuala-lumpur我使用了这个数据集: https : //www.kaggle.com/dragonduck/property-listings-in-kuala-lumpur
You could use tidyr::separate
which.您可以使用
tidyr::separate
which。 handles this cleanly :干净地处理这个:
splitingRoomsDF <- tidyr::separate(out, Rooms, into = c('Room1', 'Room2'),
sep = "\\+",remove = FALSE, convert = TRUE, fill = "right")
head(splitingRoomsDF[, 1:5])
# Location Price Rooms Room1 Room2
#1 KLCC, Kuala Lumpur RM 1,250,000 2+1 2 1
#2 Damansara Heights, Kuala Lumpur RM 6,800,000 6 6 NA
#3 Dutamas, Kuala Lumpur RM 1,030,000 3 3 NA
#4 Cheras, Kuala Lumpur NA NA
#5 Bukit Jalil, Kuala Lumpur RM 900,000 4+1 4 1
#6 Taman Tun Dr Ismail, Kuala Lumpur RM 5,350,000 4+2 4 2
If you want to set NA
's to 0, you could do如果你想将
NA
设置为 0,你可以这样做
splitingRoomsDF[4:5][is.na(splitingRoomsDF[4:5])] <- 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.