HI, I have a dataframe with two variable A, B. I transform the two variable and name them as C, D and save it in a dataframe dfcd. However, I wonder why can't I call them by dfcd$C and dfcd$D? Thanks, Miao> A=c(1,2,3) > B=c(4,6,7) > dfab<-data.frame(A,B) > C=dfab["A"]*2 > D=dfab["B"]*3 > dfcd<-data.frame(C,D) > dfcdA B 1 2 12 2 4 18 3 6 21> dfcd$CNULL> dfcd$A[1] 2 4 6 [[alternative HTML version deleted]]
Hi, Because a column name exists for "C" and "D": > colnames(C) [1] "A" > colnames(D) [1] "B" One possibility: > A=c(1,2,3) > B=c(4,6,7) > dfab<-data.frame(A,B) > C=dfab$A*2 > D=dfab$B*3 > dfcd<-data.frame(C,D) > dfcd C D 1 2 12 2 4 18 3 6 21 > dfcd$C [1] 2 4 6 HTH, Pascal On 04/17/2013 02:33 PM, jpm miao wrote:> HI, > I have a dataframe with two variable A, B. I transform the two variable > and name them as C, D and save it in a dataframe dfcd. However, I wonder > why can't I call them by dfcd$C and dfcd$D? > > Thanks, > > Miao > >> A=c(1,2,3) >> B=c(4,6,7) >> dfab<-data.frame(A,B) >> C=dfab["A"]*2 >> D=dfab["B"]*3 >> dfcd<-data.frame(C,D) >> dfcd > A B > 1 2 12 > 2 4 18 > 3 6 21 >> dfcd$C > NULL >> dfcd$A > [1] 2 4 6 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
On Apr 16, 2013, at 10:33 PM, jpm miao wrote:> HI, > I have a dataframe with two variable A, B. I transform the two > variable > and name them as C, D and save it in a dataframe dfcd. However, I > wonder > why can't I call them by dfcd$C and dfcd$D?Because you didn't assign them to dfab$C. It's going to be more successful if you use: dfab[["A"]]*2> > Thanks, > > Miao > >> A=c(1,2,3) >> B=c(4,6,7) >> dfab<-data.frame(A,B) >> C=dfab["A"]*2 >> D=dfab["B"]*3 >> dfcd<-data.frame(C,D) >> dfcd > A B > 1 2 12 > 2 4 18 > 3 6 21 >> dfcd$C > NULL >> dfcd$A > [1] 2 4 6 > > [[alternative HTML version deleted]]David Winsemius, MD Alameda, CA, USA
Hi, You may also try: dfab<-data.frame(A,B) library(plyr) ?dfcd<-subset(mutate(dfab,C=A*2,D=B*3),select=-c(A,B)) #or ?dfcd1<-subset(within(dfab,{D<-B*3;C<-A*2}),select=-c(A,B)) dfcd$C #[1] 2 4 6 ?dfcd$D #[1] 12 18 21 A.K. ----- Original Message ----- From: jpm miao <miaojpm at gmail.com> To: r-help <r-help at r-project.org> Cc: Sent: Wednesday, April 17, 2013 1:33 AM Subject: [R] Transformation of a variable in a dataframe HI, ? I have a dataframe with two variable A, B. I transform the two variable and name them as C, D and save it in a dataframe? dfcd. However, I wonder why can't I call them by dfcd$C and dfcd$D? ? Thanks, Miao> A=c(1,2,3) > B=c(4,6,7) > dfab<-data.frame(A,B) > C=dfab["A"]*2 > D=dfab["B"]*3 > dfcd<-data.frame(C,D) > dfcd? A? B 1 2 12 2 4 18 3 6 21> dfcd$CNULL> dfcd$A[1] 2 4 6 ??? [[alternative HTML version deleted]] ______________________________________________ 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.
Seemingly Similar Threads
- Why can't R understand if(num!=NA)?
- Definition of "lag" is opposite in ts and xts objects!
- How can I tabulate time series data (in RStudio or any other R editor)?
- Declare a set (list?) of many dataframes or matrices
- How to print the frequency table (produced by the command "table" to Excel