Hi, I am a bit rusty with R programming and do not seem to find a solution to add a number of variables to my existing dataframe. Basically I need to add n=dim(d1)[1] variables to my d0 dataframe and I would like them to be named V1, V2, V3, ... , V[dim(d1)[1]) When running the following code: for (t in 1:dim(d1)[1]){ d0$V[t] <- 0 } all I get is a V variable populated with zeros... I am sure there is a fairly straightforward code to accomplish what I need, any suggestion? Thank you, Luca [[alternative HTML version deleted]]
Hi Luca, How about this? # create some dummy data since I don't have your d0 or d1> n <- 3 > d0 <- data.frame(a=runif(5),b=runif(5))# here's the suggested code> d1 <- cbind(d0, matrix(0,nrow(d0),n)) > colnames(d1)[1:n + ncol(d0)] <- paste("V",1:n,sep="")HTH, Eric On Sun, Apr 22, 2018 at 11:13 AM, Luca Meyer <lucam1968 at gmail.com> wrote:> Hi, > > I am a bit rusty with R programming and do not seem to find a solution to > add a number of variables to my existing dataframe. Basically I need to add > n=dim(d1)[1] variables to my d0 dataframe and I would like them to be named > V1, V2, V3, ... , V[dim(d1)[1]) > > When running the following code: > > for (t in 1:dim(d1)[1]){ > d0$V[t] <- 0 > } > > all I get is a V variable populated with zeros... > > I am sure there is a fairly straightforward code to accomplish what I need, > any suggestion? > > Thank you, > > Luca > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]
Another option is d0[paste0("V", 1:nrow(d1))] <- 0 --Ista On Sun, Apr 22, 2018 at 4:23 AM, Eric Berger <ericjberger at gmail.com> wrote:> Hi Luca, > How about this? > > # create some dummy data since I don't have your d0 or d1 >> n <- 3 >> d0 <- data.frame(a=runif(5),b=runif(5)) > > # here's the suggested code >> d1 <- cbind(d0, matrix(0,nrow(d0),n)) >> colnames(d1)[1:n + ncol(d0)] <- paste("V",1:n,sep="") > > HTH, > Eric > > > On Sun, Apr 22, 2018 at 11:13 AM, Luca Meyer <lucam1968 at gmail.com> wrote: > >> Hi, >> >> I am a bit rusty with R programming and do not seem to find a solution to >> add a number of variables to my existing dataframe. Basically I need to add >> n=dim(d1)[1] variables to my d0 dataframe and I would like them to be named >> V1, V2, V3, ... , V[dim(d1)[1]) >> >> When running the following code: >> >> for (t in 1:dim(d1)[1]){ >> d0$V[t] <- 0 >> } >> >> all I get is a V variable populated with zeros... >> >> I am sure there is a fairly straightforward code to accomplish what I need, >> any suggestion? >> >> Thank you, >> >> Luca >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Like this?> V <- c("fee","fie","foe") > aq <- head(airquality) # Just to get a shorter example > aq[V] <- 0 > aqOzone Solar.R Wind Temp Month Day fee fie foe 1 41 190 7.4 67 5 1 0 0 0 2 36 118 8.0 72 5 2 0 0 0 3 12 149 12.6 74 5 3 0 0 0 4 18 313 11.5 62 5 4 0 0 0 5 NA NA 14.3 56 5 5 0 0 0 6 28 NA 14.9 66 5 6 0 0 0> On 22 Apr 2018, at 10:13 , Luca Meyer <lucam1968 at gmail.com> wrote: > > Hi, > > I am a bit rusty with R programming and do not seem to find a solution to > add a number of variables to my existing dataframe. Basically I need to add > n=dim(d1)[1] variables to my d0 dataframe and I would like them to be named > V1, V2, V3, ... , V[dim(d1)[1]) > > When running the following code: > > for (t in 1:dim(d1)[1]){ > d0$V[t] <- 0 > } > > all I get is a V variable populated with zeros... > > I am sure there is a fairly straightforward code to accomplish what I need, > any suggestion? > > Thank you, > > Luca > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Possibly Parallel Threads
- How to dynamically add variables to a dataframe
- How to visualise what code is processed within a for loop
- How to visualise what code is processed within a for loop
- How to visualise what code is processed within a for loop
- How to visualise what code is processed within a for loop