[R] some help

Rui Barradas ruipbarradas at sapo.pt
Sun Nov 4 13:03:38 CET 2012


Hello,

Why answer to just me? You should keep it in the R-Help list, the odds 
of getting more (and hopefully better) answers is bigger.

Anyway, with data examples it's much easier to do something. I hope the 
code is self explanatory, except maybe for the use of scan(), not 
read.table. scan() reads in vectors, so there's no need to stack the 
columns anymore, they're just one big vector. Another thing is that 
around half of the values are zeros, and those don't show up in the 
graph you've posted, so I've filtered them out.

fdir <- "Asciis"  # change if needed
curr_dir <- setwd(fdir)  # save current directory and set working dir
fls <- list.files()
lst <- scan(fls[1], dec=",")
ndv <- scan(fls[2], dec=",")

str(lst)
str(ndv)

i0 <- ndv > 0 & lst > 0
ndv0 <- ndv[i0]
lst0 <- lst[i0]

brks <- seq(0, 1, by = 0.01)
group <- cut(ndv0, breaks = brks)

mins <- tapply(lst0, group, FUN = min) # one min per group
maxs <- tapply(lst0, group, FUN = max) # one max per group

fit.min <- lm(mins~brks[-1L])
fit.max <- lm(maxs~brks[-1L])

dev.new()
plot(ndv0, lst0, pch=".")
abline(fit.min, col = "blue")
abline(fit.max, col = "red")

#----  Now with ave(). It returns one min/max per element in lst0,
#----  so you'll have a vector as long as the original lst0, with the 
min/max
#----  corresponding values.

mins2 <- ave(lst0, group, FUN = min)  # one min per element of lst0
maxs2 <- ave(lst0, group, FUN = max)  # one max per element of lst0

fit.min2 <- lm(mins2~ndv0)
fit.max2 <- lm(maxs2~ndv0)

dev.new()
plot(ndv0, lst0, pch=".")
abline(fit.min2, col = "blue")
abline(fit.max2, col = "red")


Hope this helps,

Rui Barradas
Em 04-11-2012 10:36, Stefan Mühlbauer escreveu:
> Hello Rui,
>
> Thanks a lot for your answer.
> I can also provide you some data:
>
> It's satellite images as a text file (ascii). Two different images with two different variables, land surface temperature (LST -lstascii =filename) and an vegetation index (NDVI - ndviascii = filename).
> The aim was to do a scatter plot of NDVI against LST, whereas the NDVI should be on the x-axis. - The scatter plot itself is not needed as you will see.
>
> As I cannot compare each value (pixel value) in one image to the respective value in the other image, i had the idea to sort all columns one under each other. In the end I would have one column of one variable. I wanted to put the two variables LST and NDVI together in one table, so that I have two columns with two variables, and the value of one variable (NDVI) in each row can be compared directly to the value of the other variable (LST) in the same row. Why that? I wanted to sort the columns by the NDVI variable and make 0,01 NDVI intervals. From each of these intervals I wanted to know the max and min LST. These values I need for the regression - see the graph in the file attached.
>
>
> Can you understand this?
> Aattached you can see the ascii files of LST and NDVI.
>
> Again thanks a lot for help!
>
> Best Regards
>
> Stefan
>
>
>   
>                                                 
>
> Dipl.-Ing. Stefan Mühlbauer
>
> Kaiser Strasse 85/2/15
> A - 1070 Wien
>
> E-Mail:
> stefan.muehlb at yahoo.de
> dattel_palme at yahoo.de
>
>
>
> ________________________________
>   Von: Rui Barradas <ruipbarradas at sapo.pt>
> An: dattel_palme <dattel_palme at yahoo.de>
> CC: r-help at r-project.org
> Gesendet: 19:34 Samstag, 3.November 2012
> Betreff: Re: [R] some help
>   
> Hello,
>
> Without data it's not easy to answer to your questions, but
>
> 1. Use ?unlist. If the data is in a file, read it with ?read.table and
> the unlist the result. All columns will be stacked.
>
> dat <- read.table(filename, ...)
> unlist(dat)
>
> 2. At best confusing. But to divide a vector into groups use ?cut or
> ?findInterval and then, to find the maximum and minimum of each group,
> ?tapply or ?ave.
>
> 3. Regress what on what?
>
>
> Provide a data example using dput for better answers:
>
> dput( head(mydata, 30) )  # paste the output of this in a post
>
>
> Hope this helps,
>
> Rui Barradas
> Em 03-11-2012 16:07, dattel_palme escreveu:
>> Hi People!
>>
>> I have following concern consisting of some steps to do in R:
>>
>> I have an ascii file (table) consisting of many columns and rows.
>> 1. I would like to order all values of the columns one under each other. It
>> will begin with column 1, then column 2 under column 1, column 3 under
>> column 2 etc. until at the end there is only 1 column. How do I do it?
>>
>> 2. Second problem is to make a scatterplot of two variables (I think after
>> further explanation scatter plot itself will not be needed). I have two
>> columns of two different variables (that I produces before), column 1 with
>> variable 1 and column 2 with variable 2. I would like to order them by one
>> variable and 0,01 interval (the varibale values will range between 0 and 1).
>> >From each 0,01 interval (100 intervals) i want to pick the maximum and
>> minimum value of variable 2.
>>
>> 3. From the obtained max and min of values of each interval i would like to
>> make a linear least square regression.
>>
>> I hope someone can help me out!
>> Thanks
>> Stefan
>>
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/some-help-tp4648316.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list