Would like a script that removes repeat data
2 次查看(过去 30 天)
显示 更早的评论
I'm looking to create a script that removes dates that repeat one after the other. For some reason, the program I used to collect the data does something stupid where they send a prompt twice on the same day, but I only want the program to be sent once. For the repeat dates, I want those dates to be deleted. For example:
Dates_Wrong = ['2/4/21';'2/5/21';'2/5/21';'2/6/21';'2/7/21']
You can see here, the 2/5/21 date repeats. I would like to create a script that eliminates that repeat data.
The hard part is that you can't just do unique(x) on the entire dates column because there are different subjects with repeating dates and that is why I'm having trouble. It has to be something where it identifies 2 repeating dates in sequence and removes the more recent date. Here is an example of what our previous dates would look like with the repeat date removed.
Dates_Right = ['2/4/21';'2/5/21';'2/6/21';'2/7/21']
This is sort of what I was thinking of doing but I'm not sure if it makes sense
for x=1:length(MorningPrompt.SurveyStartedDate)
if x-1==x %This is where I'm having trouble. I think the rest of the script is fine but I'm not sure how to use this part to account for strings since x isn't the actually string found within that variable
MorningPrompt(x,:) = [];
end
end
0 个评论
采纳的回答
Steven Lord
2022-9-23
Dates_Wrong = ['2/4/21';'2/5/21';'2/5/21';'2/6/21';'2/7/21']
dt = datetime(Dates_Wrong, 'InputFormat', 'M/d/yy')
differences = diff(dt)
repeated = differences ~= 0
Note that differences and repeated are both one element shorter than dt. Add a true as the first or last element depending on whether you always want to keep the first element or the last.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!