Вот еще одно решение, использующее функцию bin_data()
из пакета mltools .
library(mltools)
cosinFcolor <- c(0.77, 0.067, 0.514, 0.102, 0.56, 0.029)
binned <- bin_data(cosinFcolor, bins=c(0, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0), boundaryType = "[lorc")
binned
[1] (0.7, 0.8] [0, 0.5] (0.5, 0.6] [0, 0.5] (0.5, 0.6] [0, 0.5]
Levels: [0, 0.5] < (0.5, 0.6] < (0.6, 0.7] < (0.7, 0.8] < (0.8, 0.9] < (0.9, 1]
# Convert to numbers 0, 1, ...
as.integer(binned) - 1L
df <- read.table(textConnection(
"cosinFcolor cosinEdge cosinTexture histoFcolor histoEdge histoTexture jaccard
0.770 0.489 0.388 0.57500000 0.5845137 0.3920000 0.00000000
0.067 0.496 0.912 0.13865546 0.6147309 0.6984127 0.00000000
0.514 0.426 0.692 0.36440678 0.4787535 0.5198413 0.05882353
0.102 0.430 0.739 0.11297071 0.5288008 0.5436508 0.00000000
0.560 0.735 0.554 0.48148148 0.8168083 0.4603175 0.00000000
0.029 0.302 0.558 0.08547009 0.3928234 0.4603175 0.00000000"
), sep = " ", header = TRUE)
for(col in colnames(df)) df[[col]] <- as.integer(bin_data(df[[col]], bins=c(0, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0), boundaryType = "[lorc")) - 1L
df
cosinFcolor cosinEdge cosinTexture histoFcolor histoEdge histoTexture jaccard
1 3 0 0 1 1 0 0
2 0 0 5 0 2 2 0
3 1 0 2 0 0 1 0
4 0 0 3 0 1 1 0
5 1 3 1 0 4 0 0
6 0 0 1 0 0 0 0
Что-то вроде:
static DateTime GetDate(int year, int month, DayOfWeek dayOfWeek,
int weekOfMonth) {
// TODO: some range checking (>0, for example)
DateTime day = new DateTime(year, month, 1);
while (day.DayOfWeek != dayOfWeek) day = day.AddDays(1);
if (weekOfMonth > 0) {
return day.AddDays(7 * (weekOfMonth - 1));
} else { // treat as last
DateTime last = day;
while ((day = day.AddDays(7)).Month == last.Month) {
last = day;
}
return last;
}
}
ИЗМЕНЕНО, чтобы исправить ошибку, когда запрошенный день недели был таким же, как и день недели первого месяца.
2-е изменение для исправления проблемы с диском 'автор: Марк
static DateTime GetDate(int year, int month,
DayOfWeek weekDay, int week)
{
DateTime first = new DateTime(year, month, 1);
int iDow = (int)weekday, iFirst = (int)first.DayOfWeek;
int adjust = (7+iDow-iFirst)%7 - 7;
return first.AddDays(7*week + adjust);
}
using System;
namespace date_using_week_of_month
{
public class Example
{
public static DateTime WthDayDOfMonthM( int w, DayOfWeek d, DateTime month )
{
return first( d, month ).AddDays( 7 * (w - 1) );
}
private static DateTime first( DayOfWeek d, DateTime month )
{
DateTime first = new DateTime(
month.Year, month.Month, 1 );
while ( first.DayOfWeek != d )
{
first = first.AddDays( 1 );
}
return first;
}
}
}