Hi all, I'm hoping there's a quick fix for this. I have two data frames and am running a loop to match similar rows. However the matching is not working well, and I suspect it is because of the different formats of columns. For example, in col. 1 of dataframe 1 the nummbers are formatted as 1,2,3,4,5,6,7,8,9...31 However in col. 1 dataframe 2 the numbers are 01,02,03,04,05,06,07,08,09...31 After investigation it seems only the numbers below 10 do not match up well (i.e. those with a 0 in). My question is: is there a quick fix so I can convert the first set of numbers into the format of the second? I could write a loop and if statement but that seems a lot and I thought there must be an easier way out there. However having searched the archives, nothing has appeared. Thanks Jamie
I would guess that your first column is factors. So an 'str' on the dataframe to see what it is made of. You can convert them to numeric: df[[1]] <- as.numeric(as.character(df[[1]])) See if you get any NAs for non-numerics that may be in that column On 4/7/08, Jamie Ledingham <jamie.ledingham at newcastle.ac.uk> wrote:> Hi all, > I'm hoping there's a quick fix for this. > > I have two data frames and am running a loop to match similar rows. > However the matching is not working well, and I suspect it is because of > the different formats of columns. For example, in col. 1 of dataframe 1 > the nummbers are formatted as 1,2,3,4,5,6,7,8,9...31 However in col. 1 > dataframe 2 the numbers are 01,02,03,04,05,06,07,08,09...31 After > investigation it seems only the numbers below 10 do not match up well > (i.e. those with a 0 in). > > My question is: is there a quick fix so I can convert the first set of > numbers into the format of the second? I could write a loop and if > statement but that seems a lot and I thought there must be an easier way > out there. However having searched the archives, nothing has appeared. > > Thanks > Jamie > > ______________________________________________ > 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 Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
> I have two data frames and am running a loop to match similar rows. > However the matching is not working well, and I suspect it is because of > the different formats of columns. For example, in col. 1 of dataframe 1 > the nummbers are formatted as 1,2,3,4,5,6,7,8,9...31 However in col. 1 > dataframe 2 the numbers are 01,02,03,04,05,06,07,08,09...31 After > investigation it seems only the numbers below 10 do not match up well > (i.e. those with a 0 in). > > My question is: is there a quick fix so I can convert the first set of > numbers into the format of the second? I could write a loop and if > statement but that seems a lot and I thought there must be an easier way > out there. However having searched the archives, nothing has appeared.Method 1: Use numbers that are of type numeric, rather than strings with digits in them. Take a look at ?as.numeric for the conversion. Method 2: Reformat the strings using format, or formatC. These both accept vector inputs, so you can format a whole column with one command. Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}