[R] List extraction
I have created a list of tables with the same columns but different number of row. Example (actual list has ~200 elements): temp1- data.frame(ID=c(Herb,Shrub),stat=c(4,5),pvalue=c(.03,.04)) temp2- data.frame(ID=c(Herb,Shrub, Tree),stat=c(12,15,13),pvalue=c(.2,0.4,.3)) L-list(a=temp1,b=temp2) L $a ID stat pvalue 1 Herb4 0.03 2 Shrub5 0.04 $b ID stat pvalue 1 Herb 120.2 2 Shrub 150.4 3 Tree 130.3 I want to use apply() or some other vectorized function to create a data.frame that appends/binds all rows from all elements, such that I get: ID stat pvalue 1 Herb4 0.03 2 Shrub5 0.04 3 Herb 12 0.20 4 Shrub 15 0.40 5 Tree 13 0.30 I'll forego the question of how to get the list element IDs (i.e., a, b) into the resulting table, unless you have the time thank in advance, chipper -- View this message in context: http://r.789695.n4.nabble.com/List-extraction-tp3413374p3413374.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Non-standard sorts on vectors
I have a dataset I need to sort: test.df-data.frame(Zone=c(Floodplain, Lake, Shoreline),Cover=c(50,60,70)) However, I don't want it sorted ascending/descending but in an order that I define via a vector: sort.v-data.frame(c(Lake,Shoreline,Floodplain)) I realize I could probably just create the vector of [factors] in the order I want, then assign the value: sort.v$Cover-test.df$Cover However, this is inefficient. Is there source code or a syntactical trick that will do the same more efficiently? Note: I have investigated order() and sort(), but neither seems to have an argument for defining the sorting order. Thanks in advance, Chipper -- View this message in context: http://r.789695.n4.nabble.com/Non-standard-sorts-on-vectors-tp2340431p2340431.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Using '[' as a function
I am learning R, and instead of learning by rote, I am trying to better understand the language in order to improve my programming. So any meta-information on why the following code works would be greatly appreciated... I obtained this code to extract the first record from each of a series of vectors in a list: example- list(c(1,2),c(3,4),c(4,5)) [[1]] [1] 1 2 [[2]] [1] 3 4 [[3]] [1] 4 5 sapply(example,'[',1) [1] 1 3 4 however, after perusing my book and the interweb, i remain puzzled about how '[' works as a function in sapply. -Why does R recognize '[' as a function? -Why does it need the quotes? - How does the function know to use the optional(?) argument 1 as the index location? - Any other information linking this specific example to the broader R environment? Any explanation of how this function works will be a small incremental gain in my understanding of R, so thanks in advance. Chipper -- View this message in context: http://r.789695.n4.nabble.com/Using-as-a-function-tp2307292p2307292.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Convert Row Names to data.frame column
Here is an example dataset: ZoneCover.df- data.frame(Value=c(1,2)) row.names(ZoneCover.df) - c(Floodplain1.Tree, Floodplain1.Shrub) I want to Export the Row.Names to a column in the dataframe: ZoneCover.df$ID - names(ZoneCover.df) which yields this: ZoneCover.df ValueID Floodplain1.Tree 1 Floodplain1.Tree Floodplain1.Shrub 2 Floodplain1.Shrub QUESTION: How do I remove the .Tree and .Shrub extensions from the ZoneCover$ID values? -- View this message in context: http://r.789695.n4.nabble.com/Convert-Row-Names-to-data-frame-column-tp2300758p2300758.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Reconciling 2 datasets
I have a dataframe: Empirical.df - data.frame(ID=c(MCUP1-2,MCUP2-5, MCUP3-3, MCUP4-3,MCUP5-9), Cover=c(60,40,45,68,72)) However, I only want to use a subset of Cover.df for my analysis. The samples I want to use are: Samples.v - c(MCUP1-2, MCUP4-3,MCUP5-9) How do I use indexing Empirical.df by Samples.v to return a new dataframe with only the data I am interested in? Here is what I mean: Cover.df - Empirical.df[???,] returns: Cover.df Cover.df IDCover 1 MCUP1-260 3 MCUP4-368 5 MCUP5-972 -- View this message in context: http://r.789695.n4.nabble.com/Reconciling-2-datasets-tp2300772p2300772.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] extracting results from wilcox_test (package::coin)
Recently, I ran a series of Kruskal-Wallace tests [kruskal.test()] using by() to group by site Output is a list: Herb.KW Herb.df$ID: 10-1 Kruskal-Wallis rank sum test data: Indicator_Rating by Year Kruskal-Wallis chi-squared = 15.24, df = 7, p-value = 0.03302 - Herb.df$ID: 18-1 Kruskal-Wallis rank sum test data: Indicator_Rating by Year Kruskal-Wallis chi-squared = 13.56, df = 7, p-value = 0.05963 - Herb.df$ID: 18-10 Kruskal-Wallis rank sum test data: Indicator_Rating by Year Kruskal-Wallis chi-squared = 16.65, df = 5, p-value = 0.005213 -- I used the following code to extract a vector of p.values for each list element: sapply(Herb.KW, '[[', 'p.value') 10-1 18-1 18-10 18-11 18-12 18-13 18-2 18-3 18-4 18-5 18-6 3.302e-02 5.963e-02 5.213e-03 1.321e-09 4.483e-04 2.823e-02 2.893e-03 2.535e-02 5.701e-03 3-1 37-15 37-16 37-17 37-20 37-21 37-23 37-24 37-25 37-26 3.552e-18 9.189e-01 4.051e-03 2.122e-09 1.325e-01 2.128e-03 4.543e-01 9.940e-02 1.748e-02 I then used wilcoxon_test for post-hoc analysis, which also returns a LIST with sites as elements. However, when I try to extract a pvalue from the list elements, I get the following message: sapply(Herb.Wilcox, '[[', 'p.value') Error in FUN(X[[1L]], ...) : object of type 'S4' is not subsettable First, how do I determine what the values (e.g., statistic, pvalue) of the model output are, because the reference manual does not say (unlike for kruskal.test)...is the value object named p.value or pvalue or p-value??? Is the statistic named statistic or Z.statistic or U.statistic Second, why isn't this object not subsettable even though class=list? how do i get around this problem and extract the statistic and p-value for each element in the list? -- View this message in context: http://n4.nabble.com/extracting-results-from-wilcox-test-package-coin-tp1567956p1567956.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] subset() for multiple values
This code works: subset(NativeDominant.df,!ID==37-R17) This code does not: Tree.df-subset(NativeDominant.df,!ID==c(37-R17,37-R18,10-R1,37-R21,37-R24,R7A-R1,3-R1,37-R16)) how do i get subset() to work on a range of values? -- View this message in context: http://n4.nabble.com/subset-for-multiple-values-tp1560543p1560543.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Extracting values from a list
I have run a kruskal.test() using the by() function, which returns a list of results like the following (subset of results): Herb.df$ID: 4-2 Kruskal-Wallis chi-squared = 18.93, df = 7, p-value = 0.00841 Herb.df$ID: 44-1 Kruskal-Wallis chi-squared = 4.43, df = 6, p-value = 0.6187 So then, how do extract a vector of p-values (i.e., result$p.value) for every element in the list? -- View this message in context: http://n4.nabble.com/Extracting-values-from-a-list-tp1560701p1560701.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Extracting values from a list
Thanks, as a follow-up, how do i extract the list element name (ie, 4-2 or 44-1) thanks, chipper Date: Thu, 18 Feb 2010 11:56:45 -0800 From: ml-node+1560750-540257540-69...@n4.nabble.com To: chipma...@hotmail.com Subject: Re: Extracting values from a list Try this: sapply(x, '[', 'p.value') On Thu, Feb 18, 2010 at 5:21 PM, chipmaney [hidden email] wrote: I have run a kruskal.test() using the by() function, which returns a list of results like the following (subset of results): Herb.df$ID: 4-2 Kruskal-Wallis chi-squared = 18.93, df = 7, p-value = 0.00841 Herb.df$ID: 44-1 Kruskal-Wallis chi-squared = 4.43, df = 6, p-value = 0.6187 So then, how do extract a vector of p-values (i.e., result$p.value) for every element in the list? -- View this message in context: http://n4.nabble.com/Extracting-values-from-a-list-tp1560701p1560701.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email] 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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ [hidden email] mailing list PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. View message @ http://n4.nabble.com/Extracting-values-from-a-list-tp1560701p1560750.html To unsubscribe from Extracting values from a list, click here. _ Hotmail: Trusted email with powerful SPAM protection. -- View this message in context: http://n4.nabble.com/Extracting-values-from-a-list-tp1560701p1560752.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@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.
[R] How do I vectorize this loop....
Basically I need to use the following data to calculate a squared error for each Sample based on the expected Survival for the zone. Basically, this code has Live/Dead for each sample, and I need to calculate the square error based on the Expected Mean (ie, Survival). The code looks up the expectation for each zone and applies for each sample in the zone using a loop: Data1 - data.frame(Sample=1:6, Live =c(24,25,30,31,22,23), Baseline = c(35,35,35,32,34,33),Zone = c(rep(Cottonwood,3),rep(OregonAsh,3))) Data2 - data.frame(Zone = c(Cottonwood,OregonAsh), Survival = c(0.83,0.76)) for (i in 1:nrow(Data1)) #(Yi -Ybar*Yo)^2 Data1$SquaredError[i] - (Data1$Live[i] - Data2$Survival[which(Data1$Zone[i]==Data2$Zone)]*Data1$Baseline[i])^2 My question is, can I vectorize this code to avoid the loop? Obviously, I could merge the 2 datasets first, but that would still require 2 steps and Data1 would have a bunch of redundant data. So, is there a better alternative? Is there some way I improve indexing syntax efficiency by using rownames instead of a column vector? -- View this message in context: http://www.nabble.com/How-do-I-vectorize-this-loop-tp26000933p26000933.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] axis scale issues....
I have read all the help on axis scales, which seems to be much harder to deal with than it should be, given how common the need to alter axes. Anywayyeah, suppress the axis using yaxt then call and define the new scale using axis(). The problem is, this doesn't change the actual axis, only the text. Example: plot(1:length(TreeDensity.df$Description[which(TreeDensity.df$Description == Zone.df[i])]),TreeDensity.df$CumMeanTree[which(TreeDensity.df$Description == Zone.df[i])], type = b, sub=Zone.df[i],xlab=Sample Size,ylab = Tree Density, yaxt=n) axis(side = 2, xpd=FALSE, at = pretty(range(0,450)) ) This code produces the following plot: http://www.nabble.com/file/p25665902/Presentation1.jpg Note that while the scale of the axis TEXT has changed, the actual axis scale hasn't, it still ranges across the value of the dataThat is, the ranges 0...~100 and ~200450 do not appear on the plot. One could say that the axis scale has changed, but part of the axis isn't showing up, so one could use xpd to suppress clippingHowever, then the axis either runs off the device window or something, like the following plot: http://www.nabble.com/file/p25665902/Presentation2.jpg So, what I really want to do is not the change both the range (to 0...450) AND scale (ie, shrink the length of the range(0,450) to fit the existing plot dimensions) of the axis. Any suggestion on how to accomplish this? Thanks in advance... -- View this message in context: http://www.nabble.com/axis-scale-issues-tp25665902p25665902.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] vectors levels are carried through to subsets...
I have a dataset. Initially, it has 25 levels for a certain factor, Description. However, I then subset it, because I am only interested in 2 of the 25 factors. When I subset it, I get the following. The vector lists only the two factors, yet there remain 25 levels: Quadrats.df$Description [1] Emergent 25x75 Emergent 25x75 Emergent 25x75 Emergent 25x75 Emergent 25x75 Emergent 25x75 Emergent 25x75 Emergent 25x75 Emergent 25x75 [10] Emergent 25x75 Emergent 25x75 Emergent 25x75 Emergent 25x75 Emergent 25x75 Emergent 25x75 Hydroseed 25x75 Hydroseed 25x75 Hydroseed 25x75 [19] Hydroseed 25x75 Hydroseed 25x75 Hydroseed 25x75 Hydroseed 25x75 Hydroseed 25x75 Hydroseed 25x75 Hydroseed 25x75 Hydroseed 25x75 Hydroseed 25x75 [28] Hydroseed 25x75 Hydroseed 25x75 Hydroseed 25x75 Hydroseed 25x75 25 Levels: Black Cottonwood Black Cottonwood Enhanced Emergent Emergent 25x75 Floodplain 1 Floodplain 2 Floodplain 3 Hydroseed 25x75 ... Western Red Cedar Enhanced This seems rather innocuous; however, when I run a by statement, it returns a list with 25 entries, 23 of which are of course NAis there a way to avoid this? -- View this message in context: http://www.nabble.com/vectors-levels-are-carried-through-to-subsets...-tp25667735p25667735.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Interesting function in a function problem....
#I have these data. Basically, I want to run a tapply to calculate the mean and st. err. by factor. #The problem is, I want to add a finite correction to the variance prior to calculating the standard error. #Now I know I could just do this in 3/4 steps by calculating the var, applying the correction, then calculating the StErr. #However, I am trying to learn about functions and how they work. So if you interested in puzzling the #following problem out.. #I want to integrate the fc into the tapply() #Since the fc is a different object than the tapply() object, how do I integrate the fc into the function() #call for the St.Err so that i get the formula ( fc*var(x)/length(x) )^0.5 for each factor #I believe this takes some fidgeting with function(x), but I suck at function building, #particularly in this case of embedding a function() with multiple inputs into an R function with only 1 #input, #so I need help...maybe i need to input the tapply() function into my own built function I don't know. data - data.frame(group = c(rep(A,4), rep(B,5)), value = c(40,45,24,34,234,243,435,324,244) ) fc - data.frame(group=c(A,B), fc = c(0.75, 0.85)) Mean - tapply(data$value,data$group, mean) StErr - tapply(data$value,data$group, function(x) ( var(x)/length(x) )^0.5 ) #no fc yet -- View this message in context: http://www.nabble.com/Interesting-function-in-a-function-problem-tp25672027p25672027.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] how do i vectorize relational queries in R
I am basically trying to append a value(vector) to one dataframe using a relational value from another dataframe. Obviously, I can use a loop to accomplish this. However, is there a way to vectorize it? Example: data - data.frame(c(1,1,1,2,2,2,3,3,3),rep(2,9)); names(data) - c(Sample,Score) meta - data.frame(c(1,2,3),c(Tree,Tree,Shrub)); names(meta) - c(Sample,Stratum) The following attempt at vectorizaton doesn't work: data$Stratum - meta$Stratum[which(data$Sample == meta$Sample)] data$Stratum [1] Tree NA NA Tree NA NA Tree NA NA And actually, when I try to run a loop, the operation converts the string to a factor. for (i in 1:length(data[,1])) data$Stratum[i] - meta$Stratum[which(meta$Sample == data$Sample[i])] data Sample Score Stratum 1 1 2 2 2 1 2 2 3 1 2 2 4 2 2 2 5 2 2 2 6 2 2 2 7 3 2 1 8 3 2 1 9 3 2 1 ArggI don't want a factor, and anyway I don't want to use a loop... Can anyone help with these two issues??? -- View this message in context: http://www.nabble.com/how-do-i-vectorize-relational-queries-in-R-tp25052929p25052929.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] How do I run the by function on an object of class by???
I wrote a function with some loops and avoided a third loop by using the by function. The output object is an array (or list) of class by that looks like this: StationCover.df Empirical.df$Quadrat: VFFF9-21 Station Shrub Tree Woody Invasive Herb Litter Bare [1,] 0.0 11 100 00 Empirical.df$Quadrat: VFFF9-22 Station Shrub Tree Woody Invasive Herb Litter Bare [1,] 0.0 11 100 00 Empirical.df$Quadrat: VFFF9-23 Station Shrub Tree Woody Invasive Herb Litter Bare [1,] 0.0 11 100 00 etc etc. basically, i want to use this output object, StationCover.df as a data object for another by() function. However, I get the following error: by(StationCover.df,Quadrats.df,TotalCover) Error in as.data.frame.default(data) : cannot coerce class by into a data.frame So, obviously I need to extract data.frames from my original by() function output, StationCover.df for use in other functions. Any suggestions on the quickest way to do this? -- View this message in context: http://www.nabble.com/How-do-I-run-the-by-function-on-an-object-of-class-%22by%22tp24541893p24541893.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Help get this simple function to work...
I have a function (see below). This function has one object, ID. If I run the loops by themselves using a character value (ie,VFFF1-7) instead of the function object, then the loops work fine. However, when I try to insert the character value via the function call, it doesn't work. I don't get an error, but the TotalCover.df dataframe does not update according to the loop criteria. Any obvious problems that you can see? Cover Function# #Define Variables Quadrats.df-unique(data.df$Quadrat) TotalCover.df-cbind(0:750/10,0,0,0,0,0,0) colnames(TotalCover.df)- c(Station,Shrub,Tree,Invasive,Herb,Litter,Bare) Shrub.df-data.df[data.df$Layer==Shrub,] Tree.df-data.df[data.df$Layer==Tree,] Cover.fn-function(ID){ ShrubCover.df-Shrub.df[Shrub.df$Quadrat==ID,] for (j in 1:length(ShrubCover.df[,Quadrat])){ for (i in 1:751){ if(TotalCover.df[i,Station]=ShrubCover.df[j,Start] TotalCover.df[i,Station]= ShrubCover.df[j,Stop]) TotalCover.df[i,Shrub]- 1 } } TreeCover.df-Tree.df[Tree.df$Quadrat==ID,] for (j in 1:length(TreeCover.df[,Quadrat])){ for (i in 1:751){ if(TotalCover.df[i,Station]=TreeCover.df[j,Start] TotalCover.df[i,Station]= TreeCover.df[j,Stop]) TotalCover.df[i,Tree]- 1 } } } Cover.fn(VFFF1-7) -- View this message in context: http://www.nabble.com/Help-get-this-simple-function-to-work...-tp24466533p24466533.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.