similar to: Extracting from data.frame

Displaying 20 results from an estimated 10000 matches similar to: "Extracting from data.frame"

2005 Jun 09
1
Subassignments involving NAs in data frames
I'm seeing some inconsistent behavior when re-assigning values in a data frame. The first assignment turns all of the 0s in my data frame to 2s, the second fails to do so. > df1 <- data.frame(a = c(NA, 0, 3, 4)) > df2 <- data.frame(a = c(NA, 0, 0, 4)) > df1[df1 == 0] <- 2 ## Works > df2[df2 == 0] <- 2 Error: NAs are not allowed in subscripted assignments Checking an
2012 Jan 18
2
Extracting rows with latest date from a data frame
Hi, I have a list of unique patient IDs which I want to run against a dataframe of unique patient IDs with dates. There are multiple date instances against unique IDs. The end result I wish is that I have a list of IDs (which are unique) with the latest date. I have begun with using test data (but I haven't got very far!) #Create test data #Data frame creation
2011 Aug 18
2
Best way/practice to create a new data frame from two given ones with last column computed from the two data frames?
Dear expeRts, What is the best approach to create a third data frame from two given ones, when the new/third data frame has last column computed from the last columns of the two given data frames? ## Okay, sounds complicated, so here is an example. Assume we have the two data frames: df1 <- data.frame(Year=rep(2001:2010, each=2), Group=c("Group 1","Group 2"), Value=1:20)
2012 Oct 11
1
as.data.frame.matrix() returns an invalid object
Hi, Two ways to create what should normally be the same data frame: > df1 <- data.frame(a=character(0), b=character(0))> df1 [1] a b <0 rows> (or 0-length row.names) > df2 <- as.data.frame(matrix(character(0), ncol=2, dimnames=list(NULL, letters[1:2]))) > df2 [1] a b <0 rows> (or 0-length row.names) unique() works as expected except that I
2011 Apr 30
3
indexing into a data.frame using another data.frame that also contains values for replacement
Hello all, I have a quandry I have been scratching my head about for a while. I've searched the manual and the web and have not been able to find an acceptable result, so I am hoping for some help. I have two data frames and I want to index into the first using the second, and replace the specific values I have indexed with more values from the second data.frame. I can do this
2011 Nov 30
1
Replace columns in a data.frame randomly splitted
Dear community, I'm working with the data.frame attached ( http://r.789695.n4.nabble.com/file/n4122926/df1.xls df1.xls ), let's call it df1. I typed: df1<- read.xls("C:/... dir .../df1.xls",colNames= TRUE, rowNames= TRUE) Then I splited randomly df1 using splitdf function (http://gettinggeneticsdone.blogspot.com/2011/03/splitting- dataset-revisited-keeping.html)
2007 Aug 19
2
How to parse a string into the symbol for a data frame object
I have several data frames, eg: > df1 <- data.frame(x=seq(0,10), y=seq(10,20)) > df2 <- data.frame(a=seq(0,10), b=seq(10,20)) It is common to create loops in R like this: > for(df in list(df1, df2)){ #etc. } This works fine when you know the name of the objects to put into the list. I assume that the order of the objects in the list is respected through the loop. Inside the
2012 Jun 03
2
merging single column from different dataframe
Hi all, probably really simple to solve, but having no background in programming I haven't been able to figure this out: I have two dataframes like df1 <- data.frame(names1=c('aa','ab', 'ac', 'ad'), var1=c(1,5,7,12)) df2 <- data.frame(names2=c('aa', 'ab', 'ac', 'ad', 'ae'), var2=c(3,6,9,12,15)) Now I want merge
2007 Oct 02
4
Strange names when creating a data.frame: Difference between <- and =
This is just a curiosity question. Why do the two different syntaxes for df1 and df2 give such different results in the names(dfx)? Thanks df1 <- data.frame(nas = c("A", "B" , "B" ,"C" ,"B", "A", "D"),nums = c(3, 2, 1, 1, 2, 3, 7)) df2 <- data.frame(nas <- c("A", "B" ,
2013 Jan 02
2
rbind: inconsistent behaviour with empty data frames?
The rbind on empty and nonempty data frames behaves inconsistently. I am not sure if by design. In the first example, first row is deleted, which may or may not be on purpose: df1 <- data.frame() df2 <- data.frame(foo=c(1, 2), bar=c("a", "b")) rbind(df1, df2) foo bar 2 2 b Now if we continue: df1 <- data.frame(matrix(0, 0, 2)) names(df1) <- names(df2)
2011 Jun 06
1
Merge two columns of a data frame
I have the following data: prefix <- c("cheap", "budget") roots <- c("car insurance", "auto insurance") suffix <- c("quote", "quotes") prefix2 <- c("cheap", "budget") roots2 <- c("car insurance", "auto insurance") roots3 <- c("car insurance", "auto
2008 Feb 10
11
data frame question
Hello I have 2 data frames df1 and df2. I would like to create a new data frame new_df which will contain only the common rows based on the first 2 columns (chrN and start). The column score in the new data frame should be replaced with a column containing the average score (average_score) from df1 and df2. df1= data.frame(chrN= c(“chr1”, “chr1”, “chr1”, “chr1”, “chr2”, “chr2”, “chr2”),
2008 Dec 28
2
Conditional operation on multiple columns from two data frames
Hi- I have two data frames for which I wish to conditionally subtract the values of one dataframe from the other. I want to subtract df1$x from df2$x when their id is equal and the absolute value of difference between the dates is 12 hours or less. If there is no match of equal id's and dates less than 12 hours apart I want it to return "NA". Note that df1 has missing values in x
2006 Aug 13
2
How to order or sort a data.frame
I have a dataframe where I would like to order first by variable, year, and then within that variable by month. So far the only way that I have seen to do this is to order by year and then subset year and sort by month and then do an rbind to get things back together. Is this the right approach? Example: us.state <-rep("California", 23)
2013 Feb 26
2
merging or joining 2 dataframes: merge, rbind.fill, etc.?
#I want to "merge" or "join" 2 dataframes (df1 & df2) into a 3rd (mydf). I want the 3rd dataframe to contain 1 row for each row in df1 & df2, and all the columns in both df1 & df2. The solution should "work" even if the 2 dataframes are identical, and even if the 2 dataframes do not have the same column names. The rbind.fill function seems to work. For
2008 Sep 14
5
difference of two data frames
Hello I have 2 data frames DF1 and DF2 where DF2 is a subset of DF1: DF1= data.frame(V1=1:6, V2= letters[1:6]) DF2= data.frame(V1=1:3, V2= letters[1:3]) How do I create a new data frame of the difference between DF1 and DF2 newDF=data.frame(V1=4:6, V2= letters[4:6]) In my real data, the rows are not in order as in the example I provided. Thanks much Joseph [[alternative HTML version
2007 Jul 03
2
vertically concatenating data frames
Hi, what is the recommended way to vertically concatenate 2 data frames with the same column names but different number of rows? My problem is something along these lines: df1 <- data.frame(var1=var1,var2=var2,var3=var3) # nrow(df1)=1000 df2 <- data.frame(var1=var4,var2=var5,var3=var6) # nrow(df2)=2000 I tried df <- c(df1,df2), no success. stack does not seem to be appropriate
2009 Sep 18
1
merging data frames with matrix objects when missing cases
Hi, I have faced a problem with the merge() function when trying to merge two data frames that have a common index but the second one does not have cases for all indexes in the first one. With usual variables R fills in the missing cases with NA if all=T is requested. But if the variable is a matrix R seems to insert NA only to the first column of the matrix and fill in the rest of the columns by
2009 Jan 21
3
merging several dataframes from a list
Hi there, I have a list of dataframes (generated by reading multiple files) and all dataframes are comparable in dimension and column names. They also have a common column, which, I'd like to use for merging. To give a simple example of what I have: df1 <- data.frame(c(LETTERS[1:5]), c(2,6,3,1,9)) names(df1) <- c("pos", "data") df3 <- df2 <- df1 df2$data
2004 Feb 19
1
Comparing two regression slopes
I would suggest the method of Sokal and Rholf (1995) S. 498, using the F test. Below I repeat the analysis by Spencer Graves: Spencer: > df1 <- data.frame(x=1:3, y=1:3+rnorm(3)) > df2 <- data.frame(x=1:3, y=1:3+rnorm(3)) > fit1 <- lm(y~x, df1) > s1 <- summary(fit1)$coefficients > fit2 <- lm(y~x, df2) > s2 <- summary(fit2)$coefficients > db <-