ls()
setwd('D:\\areas_protegidas\\xls_dbf')
df<-read.table('D:\\areas_protegidas\\xls_dbf\\todas_las_variables_sam.txt',sep='\t',header=T)

# extraccion de las variables para los histogramas

print(head(df))
str(df$Precipitation)
names(df)

# manual
A<-df[,c(21:length(df))] #concateno las primeras columnas
B<-data.frame(df$Precipitation)
names(B)[1] <- "PPT"
# B<-data.frame(df[,8]) #extraigo una delas variables x
C<-c(A,B)
write.table(C, file=paste0('histogramas_PPT_sam','.txt'), sep="\t",row.names = F)              

B<-data.frame(df$Temperature)
names(B)[1]<-"TMP"
C<-c(A,B)
write.table(C, file=paste0('histogramas_TMP_sam','.txt'), sep="\t",row.names = F)              

B<-data.frame(df$Elevation)
names(B)[1]<-"ELEV"
C<-c(A,B)
write.table(C, file=paste0('histogramas_ELEV_sam','.txt'), sep="\t",row.names = F)              

B<-data.frame(df$Terrain.slope)
names(B)[1]<-"SLOPE"
C<-c(A,B)
write.table(C, file=paste0('histogramas_SLOPE_sam','.txt'), sep="\t",row.names = F)              

B<-data.frame(df$Soil.fertility)
names(B)[1]<-"TEB"
C<-c(A,B)
write.table(C, file=paste0('histogramas_TEB_sam','.txt'), sep="\t",row.names = F)              

B<-data.frame(df$Population)
names(B)[1]<-"POP_COUNT"
C<-c(A,B)
write.table(C, file=paste0('histogramas_POP_COUNT_sam','.txt'), sep="\t",row.names = F)              

B<-data.frame(df$Distance.roads)
names(B)[1]<-"DIST_ROAD"
C<-c(A,B)
write.table(C, file=paste0('histogramas_ROAD_sam','.txt'), sep="\t",row.names = F)              

B<-data.frame(df$Isolation)
names(B)[1]<-"ACCESS"
C<-c(A,B)
write.table(C, file=paste0('histogramas_ACCESS_sam','.txt'), sep="\t",row.names = F)              

B<-data.frame(df$Crop.suitability)
names(B)[1]<-"CULTIVOS"
C<-c(A,B)
write.table(C, file=paste0('histogramas_CULTIVOS_sam','.txt'), sep="\t",row.names = F)              

B<-data.frame(df$Distance.to.frontiers)
names(B)[1]<-"DIST_FRONT"
C<-c(A,B)
write.table(C, file=paste0('histogramas_DIST_FRONT_sam','.txt'), sep="\t",row.names = F)              

B<-data.frame(df$Animals)
names(B)[1]<-"DIV_AN"
C<-c(A,B)
write.table(C, file=paste0('histogramas_DIV_AN_sam','.txt'), sep="\t",row.names = F)              

B<-data.frame(df$biomass_ma)
names(B)[1]<-"BIOMASS_M"
C<-c(A,B)
write.table(C, file=paste0('histogramas_BIOMASS_M_sam','.txt'), sep="\t",row.names = F)              

B<-data.frame(df$Touristiness)
names(B)[1]<-"PANO_POP"
C<-c(A,B)
write.table(C, file=paste0('histogramas_PANO_POP_sam','.txt'), sep="\t",row.names = F)              


# con un FOR
# A<-df[,c(1:5)] #concateno las primeras columnas
# B<-df[,c(8,21)] #concateno las columnas con las variables x
# for (i in col(B)) {
#   C<- c(A,i)
#   write.table(C, file=c('histogramas_',col[i],'.txt'), sep="\t")
# }
# 
# 
# 
# 
# 
# 
# 
# country<-levels(factor(df$PAIS_COD)) 
# 
# for (i in 1:length(region)){
#   df_ss <- subset(df, df$region_ell_simp ==i)
#   df_ss$region_ell_simp<-NULL #saco el campo $region_ell_simp<- NULL
#   max<-df_ss$area_gril
#   df_ss$factor_pond<-df_ss$area_gril*100/max
#   df_ss$area_gril<-NULL
#   df_ss_omit<-na.omit(df_ss)
#   pond<-df_ss_omit$factor_pond
#   write.table(pond, file=paste0('todas_las_variables_PPPPPP_',region[i],'_factor_pond.txt','.txt'), sep="\t", col.names=T, row.names=F)
#   df_ss_omit$factor_pond<-NULL
#   write.table(df_ss_omit, file=paste('todas_las_variables_PPPPP_',region[i],'_omit.txt'), col.names=T, row.names=T)
# }