#########################################################################################
# Obtener una muestra de un solo pais actualizando el valor de los pesos de las filas
# data: dataframe con los datos
# fields: vector con los nombres o índices de las columnas a extraer del dataframe
# nmax: tamaño máximo dee la muestra
# wghts: vector con los nombres o índices de las columnas que son pesos en el dataframe
# naomit: omitir filas con valores faltantes
# resultado: dataframe con las columnas indicadas y los pesos actualizados
#########################################################################################
# Obtain a sample from an unique country updating the weight value of the rows
# data: dataframe with the data
# fields: vector with names or indices of the columns to extract from dataframe
# nmax: maximum size of the sample
# wghts: vector with the names or indices of the columns that contain weights
# naomit: omit rows with mising values
# result: dataframe with the indicated columns and the weights updated
#########################################################################################

wght_sample <- function(data,fields,nmax,wghts,naomit = T) {
	if (naomit) {
		rdata<-na.omit(data[,fields]);
	}
	else {
	      rdata<-data[,fields];
	}
	if (nmax > nrow(rdata)) {
		nsamp<-nrow(rdata);
	}
	else {
		nsamp<-nmax;
	}	
	rdata<-rdata[sample(nrow(rdata),nsamp),];
	if (!is.null(wghts)) {
		if (is.numeric(wghts[0])) {
			rdata[,names(data)[wghts]] <- rdata[,names(data)[wghts]] * length(data[,1]) / length(rdata[,1]);
		}
		else {
			rdata[,wghts] <- rdata[,wghts] * length(data[,1]) / length(rdata[,1]);
		}
	}
	return(rdata);
}

#########################################################################################
# Obtener una muestra de varios paises actualizando el valor de los pesos de las filas
# data: dataframe con los datos
# cnt: nombre o índice de la columna con el país
# fields: vector con los nombres o índices de las columnas a extraer del dataframe, no debe contener la columna del pais
# nmax: tamaño máximo dee la muestra
# wghts: vector con los nombres o índices de las columnas que son pesos en el dataframe
# naomit: omitir filas con valores faltantes
# resultado: dataframe con las columnas indicadas y los pesos actualizados
#########################################################################################
# Obtain a sample from multiple countries updating the weight value of the rows
# data: dataframe with the data
# cnt: name or index of the column with the country
# fields: vector with names or indices of the columns to extract from dataframe, do not add the country column
# nmax: maximum size of the sample
# wghts: vector with the names or indices of the columns that contain weights
# naomit: omit rows with mising values
# result: dataframe with the indicated columns and the weights updated
#########################################################################################

wght_multiple_sample <- function(data,cnt,fields,nmax,wghts,naomit = T) {
	lindex<-levels(data[,cnt]);
	if (naomit) {
		rdata<-na.omit(data[,c(cnt,fields)]);
	}
	else {
		rdata<-data[,c(cnt,fields)];
	}
	rsamp<-subset(rdata,is.na(cnt));
	for (i in 1:length(lindex)) {
		total<-length(data[data[,cnt]==lindex[i],1]);
		subs<-subset(rdata,rdata[,1]==lindex[i]);
		if (nmax > nrow(subs)) {
			nsamp<-nrow(subs);
		}
		else {
			nsamp<-nmax;
		}	
		isamp<-subs[sample(nrow(subs),nsamp),];
		if (!is.null(wghts)) {
			if (is.numeric(wghts[0])) {
				isamp[,names(data)[wghts]] <- isamp[,names(data)[wghts]] * total / length(isamp[,1]);
			}
			else {
		   		isamp[,wghts] <- isamp[,wghts] * total / length(isamp[,1]);
			}
		}
		rsamp<-rbind(rsamp,isamp);
	}
	return(rsamp);
}
#########################################################################################
# Obtener una muestra de un determinado pais actualizando el valor de los pesos de las filas
# data: dataframe con los datos
# cnt: nombre o índice de la columna con el país
# cntval: valor del país a muestrear
# fields: vector con los nombres o índices de las columnas a extraer del dataframe, no debe contener la columna del pais
# nmax: tamaño máximo dee la muestra
# wghts: vector con los nombres o índices de las columnas que son pesos en el dataframe
# naomit: omitir filas con valores faltantes
# resultado: dataframe con las columnas indicadas y los pesos actualizados
#########################################################################################
# Obtain a sample from a determined country updating the weight value of the rows
# data: dataframe with the data
# cnt: name or index of the column with the country
# cntval: value of the country to sample
# fields: vector with names or indices of the columns to extract from dataframe, do not add the country column
# nmax: maximum size of the sample
# wghts: vector with the names or indices of the columns that contain weights
# naomit: omit rows with mising values
# result: dataframe with the indicated columns and the weights updated
#########################################################################################

wght_cnt_sample <- function(data,cnt,cntval,fields,nmax,wghts,naomit = T) {
	if (naomit) {
		rdata<-na.omit(data[,c(cnt,fields)]);
	}
	else {
		rdata<-data[,c(cnt,fields)];
	}
	rsamp<-subset(rdata,is.na(cnt));
	if (is.numeric(cnt)) {
		subs<-subset(rdata,rdata[,names(data)[cnt]]==cntval);
	}
	else {
		subs<-subset(rdata,rdata[,cnt]==cntval);
	}
	if (nmax > nrow(subs)) {
		nmax <- nrow(subs);
	}
	rsamp<-subs[sample(nrow(subs),nmax),];
	if (!is.null(wghts)) {
		if (is.numeric(wghts[0])) {
			rsamp[,names(data)[wghts]] <- rsamp[,names(data)[wghts]] * length(data[,1]) / length(rsamp[,1]);
		}
		else {
		   	rsamp[,wghts] <- rsamp[,wghts] * length(data[,1]) / length(rsamp[,1]);
		}
	}
	return(rsamp);
}
