Hi, I have a dataframe SDF1 that looks like this: Char1 Char2 Char 3 W.2007.02 W.2007.09 W.2007.16 W.2008.13 A C1 F1 F2 F3 A C2 F4 B C3 F5 F6 I have another dataframe SDF2 with 163 cols that has the following column names Char1 Char2 Char 3 W.2007.02 W.2007.03 W.2007.04 W.2007.05 W.2007.06 .W.2007.52 W.2008.01 . W.2008.13 Now I would like to add the missing period names to SDF1 as new columns and in the same order as in SDF2. Therefore I use the following formula: [[alternative HTML version deleted]]
Jim, The problem with your solution is that the columnames are not in the same order as in the dataframe SDF2 The "dummy columns" are just put at the end of the dataframe SDF1. I keep looking for a better solutions. Thx for the effort. Bert _____ From: jim holtman [mailto:jholtman@gmail.com] Sent: 15 May 2008 18:12 To: Bert Jacobs Subject: Re: [R] Adding columns to dataframe Will somethink like this work to just add dummy columns:> x <- data.frame(a=1:10, b=1:10) > y <- data.frame(a=1, c=1, d=1, e=1, f=1) > # get unique names in y > uniq <- setdiff(names(y), names(x)) > # add dummy columns to x > x[uniq] <- 0 > xa b c d e f 1 1 1 0 0 0 0 2 2 2 0 0 0 0 3 3 3 0 0 0 0 4 4 4 0 0 0 0 5 5 5 0 0 0 0 6 6 6 0 0 0 0 7 7 7 0 0 0 0 8 8 8 0 0 0 0 9 9 9 0 0 0 0 10 10 10 0 0 0 0>On Thu, May 15, 2008 at 12:07 PM, Bert Jacobs <b.jacobs@pandora.be> wrote: Apparently the formula I use at the moment didn't pass through. As you see I use some kind of merge function. DFNEW <- merge(SDF2[,4:163],SDF1[,4:ncol(SDF1)], all.y = TRUE,all.x FALSE)[names(SDF2[,4:163])] Bert _____ From: Bert Jacobs [mailto:b.jacobs@pandora.be] Sent: 15 May 2008 18:02 To: 'jim holtman' Subject: RE: [R] Adding columns to dataframe Hi Jim, The data in the new created dataframe looks OK to me, it's only a the row position that has been changed, as if the merge evolve in some kind of sorting system. The idea is simply to add empty dummy columns, but the order of the column names should be the same as in the dataframe SDF2 Bert _____ From: jim holtman [mailto:jholtman@gmail.com] Sent: 15 May 2008 17:41 To: Bert Jacobs Subject: Re: [R] Adding columns to dataframe Does the data in the two dataframes match so that as the new columns are added the data is correct? Or do you just want dummy (empty) columns added? Can you use 'merge'? On Thu, May 15, 2008 at 11:34 AM, Bert Jacobs <b.jacobs@pandora.be> wrote: Hi, I have a dataframe SDF1 that looks like this: Char1 Char2 Char 3 W.2007.02 W.2007.09 W.2007.16 W.2008.13 A C1 F1 F2 F3 A C2 F4 B C3 F5 F6 I have another dataframe SDF2 with 163 cols that has the following column names Char1 Char2 Char 3 W.2007.02 W.2007.03 W.2007.04 W.2007.05 W.2007.06 .W.2007.52 W.2008.01 . W.2008.13 Now I would like to add the missing period names to SDF1 as new columns and in the same order as in SDF2. Therefore I use the following formula: [[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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
Jim, I think I'm almost there. When I use your formula it works and I can get the job done, but I encounter the following problem uniq <- setdiff(names(y), names(x)) OK x[uniq] <- 0 OK x NOT OK: Error in 1:ncol(mat) : NA/NaN argument fix(x) This works, but I can't use it in a function Any idea what could cause this issue? _____ From: jim holtman [mailto:jholtman@gmail.com] Sent: 15 May 2008 18:12 To: Bert Jacobs Subject: Re: [R] Adding columns to dataframe Will somethink like this work to just add dummy columns:> x <- data.frame(a=1:10, b=1:10) > y <- data.frame(a=1, c=1, d=1, e=1, f=1) > # get unique names in y > uniq <- setdiff(names(y), names(x)) > # add dummy columns to x > x[uniq] <- 0 > xa b c d e f 1 1 1 0 0 0 0 2 2 2 0 0 0 0 3 3 3 0 0 0 0 4 4 4 0 0 0 0 5 5 5 0 0 0 0 6 6 6 0 0 0 0 7 7 7 0 0 0 0 8 8 8 0 0 0 0 9 9 9 0 0 0 0 10 10 10 0 0 0 0>On Thu, May 15, 2008 at 12:07 PM, Bert Jacobs <b.jacobs@pandora.be> wrote: Apparently the formula I use at the moment didn't pass through. As you see I use some kind of merge function. DFNEW <- merge(SDF2[,4:163],SDF1[,4:ncol(SDF1)], all.y = TRUE,all.x FALSE)[names(SDF2[,4:163])] Bert _____ From: Bert Jacobs [mailto:b.jacobs@pandora.be] Sent: 15 May 2008 18:02 To: 'jim holtman' Subject: RE: [R] Adding columns to dataframe Hi Jim, The data in the new created dataframe looks OK to me, it's only a the row position that has been changed, as if the merge evolve in some kind of sorting system. The idea is simply to add empty dummy columns, but the order of the column names should be the same as in the dataframe SDF2 Bert _____ From: jim holtman [mailto:jholtman@gmail.com] Sent: 15 May 2008 17:41 To: Bert Jacobs Subject: Re: [R] Adding columns to dataframe Does the data in the two dataframes match so that as the new columns are added the data is correct? Or do you just want dummy (empty) columns added? Can you use 'merge'? On Thu, May 15, 2008 at 11:34 AM, Bert Jacobs <b.jacobs@pandora.be> wrote: Hi, I have a dataframe SDF1 that looks like this: Char1 Char2 Char 3 W.2007.02 W.2007.09 W.2007.16 W.2008.13 A C1 F1 F2 F3 A C2 F4 B C3 F5 F6 I have another dataframe SDF2 with 163 cols that has the following column names Char1 Char2 Char 3 W.2007.02 W.2007.03 W.2007.04 W.2007.05 W.2007.06 .W.2007.52 W.2008.01 . W.2008.13 Now I would like to add the missing period names to SDF1 as new columns and in the same order as in SDF2. Therefore I use the following formula: [[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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]