swonder03
2011-Oct-13 03:58 UTC
[R] Comparing the components of a data frame without levels interfering
*Bottom Line: How can I compare the individual components of two data frames with different row lengths without the levels interfering?* Example: I have two data frames to those respectively named 'dfCity' and 'dfState' that have the following output: dfCity Name City 1 Bill Detroit 2 Jody Chicago 3 Frank Memphis 4 Ron Houston dfState Name State 1 Bill Michigan 2 Frank Tennessee I have an if statement that is trying to compare the individual's names and combine the data from the two different data frames to produce some output: for(j in 1:length(row.names(dfState))){ for(k in 1:length(row.names(dfCity))){ if(dfCity[k,1] == dfState[j,1]){ cityState <- paste(dfCity[k,2], ", ", dfState[j,2], sep= "") print(dfCity[1, j], " is from ", cityState) } } } However, when I run it I get the error /Error in Ops.factor(dfState[2, 1], dfCity[4, 1]) : level sets of factors are different/ due to the levels in the data frames. *How can I compare the individual components of the data frame without the levels interfering? * -- View this message in context: http://r.789695.n4.nabble.com/Comparing-the-components-of-a-data-frame-without-levels-interfering-tp3900502p3900502.html Sent from the R help mailing list archive at Nabble.com.
Petr PIKAL
2011-Oct-13 04:38 UTC
[R] Comparing the components of a data frame without levels interfering
Hi> *Bottom Line: How can I compare the individual components of two dataframes> with different row lengths without the levels interfering?* > > Example: I have two data frames to those respectively named 'dfCity' and > 'dfState' that have the following output: > > dfCity > > Name City > 1 Bill Detroit > 2 Jody Chicago > 3 Frank Memphis > 4 Ron Houston > > dfState > > Name State > 1 Bill Michigan > 2 Frank Tennessee > > I have an if statement that is trying to compare the individual's namesand> combine the data from the two different data frames to produce someoutput:> > for(j in 1:length(row.names(dfState))){ > for(k in 1:length(row.names(dfCity))){ > if(dfCity[k,1] == dfState[j,1]){ > cityState <- paste(dfCity[k,2], ", ", dfState[j,2], sep="")> print(dfCity[1, j], " is from ", cityState) > } > } > }What about first merge two data frames mergedDF <- merge(dfCity, dfState, all=TRUE) and after that you can do cityState <- paste(mergedDF$City, ", ", mergedDF$State, sep= "") or anything you want. Regards Petr> > However, when I run it I get the error /Error in Ops.factor(dfState[2,1],> dfCity[4, 1]) : level sets of factors are different/ due to the levelsin> the data frames. *How can I compare the individual components of thedata> frame without the levels interfering? * > > -- > View this message in context:http://r.789695.n4.nabble.com/Comparing-the->components-of-a-data-frame-without-levels-interfering-tp3900502p3900502.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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Jeff Newmiller
2011-Oct-13 04:40 UTC
[R] Comparing the components of a data frame without levels interfering
Factors include a comprehensive list of all possible values they can assume. Your name columns have different numbers of names, so they are really very different factors. The best solution for you is to convert those columns to character. You also ought to learn how to use the merge function before you write much code like your example. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. swonder03 <ramey.steven@gmail.com> wrote: *Bottom Line: How can I compare the individual components of two data frames with different row lengths without the levels interfering?* Example: I have two data frames to those respectively named 'dfCity' and 'dfState' that have the following output: dfCity Name City 1 Bill Detroit 2 Jody Chicago 3 Frank Memphis 4 Ron Houston dfState Name State 1 Bill Michigan 2 Frank Tennessee I have an if statement that is trying to compare the individual's names and combine the data from the two different data frames to produce some output: for(j in 1:length(row.names(dfState))){ for(k in 1:length(row.names(dfCity))){ if(dfCity[k,1] == dfState[j,1]){ cityState <- paste(dfCity[k,2], ", ", dfState[j,2], sep= "") print(dfCity[1, j], " is from ", cityState) } } } However, when I run it I get the error /Error in Ops.factor(dfState[2, 1], dfCity[4, 1]) : level sets of factors are different/ due to the levels in the data frames. *How can I compare the individual components of the data frame without the levels interfering? * -- View this message in context: http://r.789695.n4.nabble.com/Comparing-the-components-of-a-data-frame-without-levels-interfering-tp3900502p3900502.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. [[alternative HTML version deleted]]
Apparently Analagous Threads
- Problem with a if statement inside a function
- acs package: analyze data from the U.S. American Community Survey
- acs package: analyze data from the U.S. American Community Survey
- Trying to make code more efficient
- Error: missing values where TRUE/FALSE needed