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 andminimum 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.
On Nov 3, 2012, at 9:07 AM, dattel_palme wrote:> 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?something along the lines of dat <- read.table(filename, sep=<separator>, header=TRUE) stacked <- do.call(rbind, dat)> > 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),Did you now? But from the data produced above you only have one column. Is this another data-object where these column have names?> 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. >That is incoherent to this native speaker of English who is sometimes confused by presentations of problems without concrete references. An example would help greatly.> 3. From the obtained max and min of values of each interval i would like to > make a linear least square regression.Definitely need an example. Please read the Posting Guide. --> ______________________________________________ > 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.David Winsemius, MD Alameda, CA, USA
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.
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.
Hey again! I finally, after some work done before, had time to apply the code. The sorting of the table did not work well or maybe something was misunderstood. I have a table with 973 rows and 1329 col (ascii/text file). I want to sort the table that all columns are one under each other so that at the end I have 973*1329 rows and 1 col. The col should be sorted in a way that col 2 is under col 1, col 3 under col 2, col 4 under col 3 etc. I applied this code: dat <- read.table(filename, sep=<separator>, header=TRUE) stacked <- do.call(rbind, dat) unlist(dat) ..but putting dim(dat), the number of rows and col was still 973 and 1329. So seemingly it did not work as i wanted. Thanks very much for more help. Stefan -- View this message in context: http://r.789695.n4.nabble.com/some-help-tp4648316p4650828.html Sent from the R help mailing list archive at Nabble.com.
Hey The code need some corrections and I would kindly ask for help. Say: I have a table with two columns: col1=LST and col2=NDVI i would like to sort all data by NDVI. in reality the NDVI ranges between 0 and 1 (although some values might be minus also). I want to sort by NDVI values and then make 100 intervals of 0.01 ndvi. therefore the first inerval is 0.01-0.02, the second 0.02-0.03 and so on.. for each interval I would like to get the highest (max) and lowest (min) value of LST. It would be very helpful if this values can be written in a seperate table/file. Thanks you very much for help! Stefan -- View this message in context: http://r.789695.n4.nabble.com/some-help-tp4648316p4650844.html Sent from the R help mailing list archive at Nabble.com.
try this: (provide sample data next time)> # create some test data > x <- data.frame(LST = runif(1000), NDVI = runif(1000, 0, 1)) > head(x,10) # show some dataLST NDVI 1 0.86542839 0.95129647 2 0.88910058 0.75971649 3 0.44086718 0.86532140 4 0.99879370 0.05511501 5 0.02401490 0.92282834 6 0.56026534 0.80915721 7 0.65051596 0.03606430 8 0.25897388 0.61624609 9 0.07873261 0.85179368 10 0.09829056 0.91198307> # create partition values > partition <- seq(0, 1, .01) > # add column to the data to define the partition > x$part <- cut(x$NDVI, partition) > head(x)LST NDVI part 1 0.8654284 0.95129647 (0.95,0.96] 2 0.8891006 0.75971649 (0.75,0.76] 3 0.4408672 0.86532140 (0.86,0.87] 4 0.9987937 0.05511501 (0.05,0.06] 5 0.0240149 0.92282834 (0.92,0.93] 6 0.5602653 0.80915721 (0.8,0.81]> # now compute range (min/mx) for the data > xRange <- tapply(x$LST, x$part, range) > head(xRange, 10)$`(0,0.01]` [1] 0.01945995 0.83500402 $`(0.01,0.02]` [1] 0.02267906 0.69770971 $`(0.02,0.03]` [1] 0.01287795 0.75275416 $`(0.03,0.04]` [1] 0.1402162 0.9960408 $`(0.04,0.05]` [1] 0.007688249 0.691519845 $`(0.05,0.06]` [1] 0.1047314 0.9987937 $`(0.06,0.07]` [1] 0.002181767 0.990990999 $`(0.07,0.08]` [1] 0.08271319 0.87609409 $`(0.08,0.09]` [1] 0.1174585 0.8931750 $`(0.09,0.1]` [1] 0.2331289 0.8485212 On Mon, Nov 26, 2012 at 9:56 AM, dattel_palme <dattel_palme at yahoo.de> wrote:> Hey > > The code need some corrections and I would kindly ask for help. > > Say: > I have a table with two columns: > col1=LST and col2=NDVI > i would like to sort all data by NDVI. > in reality the NDVI ranges between 0 and 1 (although some values might be > minus also). > I want to sort by NDVI values and then make 100 intervals of 0.01 ndvi. > therefore the first inerval is 0.01-0.02, the second 0.02-0.03 and so on.. > for each interval I would like to get the highest (max) and lowest (min) > value of LST. > It would be very helpful if this values can be written in a seperate > table/file. > > Thanks you very much for help! > Stefan > > > > -- > View this message in context: http://r.789695.n4.nabble.com/some-help-tp4648316p4650844.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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Hi Maybe you could use stack(dat) or melt(dat) from reshape package. Regards Petr> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of dattel_palme > Sent: Monday, November 26, 2012 12:13 PM > To: r-help at r-project.org > Subject: Re: [R] some help > > Hey again! > > I finally, after some work done before, had time to apply the code. > The sorting of the table did not work well or maybe something was > misunderstood. > > I have a table with 973 rows and 1329 col (ascii/text file). I want to > sort the table that all columns are one under each other so that at the > end I have 973*1329 rows and 1 col. The col should be sorted in a way > that col 2 is under col 1, col 3 under col 2, col 4 under col 3 etc. > > I applied this code: > dat <- read.table(filename, sep=<separator>, header=TRUE) stacked <- > do.call(rbind, dat) > unlist(dat) > > ..but putting dim(dat), the number of rows and col was still 973 and > 1329. > So seemingly it did not work as i wanted. > > Thanks very much for more help. > > Stefan > > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/some-help- > tp4648316p4650828.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.
Reasonably Related Threads
- SEM standardized path coefficients
- SpatialPolygon with the max value gets no color assigned in spplot function when using "at" parameter
- [PATCH] gm107/ir: use lane 0 for manual textureGrad handling
- bug: sample( x, size, replace = TRUE, prob= skewed.probs) produces uniform sample
- use same breaks and colors, but the displayed scale are different-image.plot()