Marcelo Mariano Silva
2018-May-10 13:04 UTC
[R] Fill down a new column in data frame with a number
Hi, I am a begginer in R programming. I am traying to create a a column in my data frame filled down with a number.> df$newcolumn <- numberHow can I do it? I am considering use rep() but in this case it is necessary know the number of rows in each data base that I have and I would like to do it in a faster ( and more elegant) way. TKs [[alternative HTML version deleted]]
Why doesn't the code you posted work for you? "number" appears to be a constant, so R will fill automatically the correct length. (Note that calling your data frame df leads to confusion.)> mydf <- data.frame(a=letters[1:5], b=runif(5)) > mydfa b 1 a 0.52880425 2 b 0.02422788 3 c 0.56089766 4 d 0.99007900 5 e 0.45031033> mydf$newcolumn <- 10 > mydfa b newcolumn 1 a 0.52880425 10 2 b 0.02422788 10 3 c 0.56089766 10 4 d 0.99007900 10 5 e 0.45031033 10 If that isn't what you want the output to look like, please provide more information. Sarah On Thu, May 10, 2018 at 9:04 AM, Marcelo Mariano Silva <marcelomarianosilva at gmail.com> wrote:> Hi, > > I am a begginer in R programming. > > I am traying to create a a column in my data frame filled down with a > number. > >> df$newcolumn <- number > > How can I do it? I am considering use rep() but in this case it is > necessary know the number of rows in each data base that I have and I would > like to do it in a faster ( and more elegant) way. > > > TKs >-- Sarah Goslee http://www.functionaldiversity.org
> I am traying to create a a column in my data frame filled down with a > number. > > > df$newcolumn <- number > > How can I do it? I am considering use rep() but in this case it is > necessary know the number of rows in each data base that I have and I would > like to do it in a faster ( and more elegant) way.dframe$newcol <- rep(n, nrow(dframe)) Notes: - 'df' is the density for the F distribution; safer to avoid giving data names of functions; - 'nrow' returns the number of rows in an existing data frame or other object with dimensions. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
Hello, You don't need rep(), R will fill the entire column with the number. See the example below. dat <- data.frame(x = 1:10) dat$newnumber <- 20 dat # x newnumber #1 1 20 #2 2 20 #3 3 20 #4 4 20 #5 5 20 #6 6 20 #7 7 20 #8 8 20 #9 9 20 #10 10 20 So your code works: df$newcolumn <- number Hope this helps, Rui Barradas On 5/10/2018 2:04 PM, Marcelo Mariano Silva wrote:> Hi, > > I am a begginer in R programming. > > I am traying to create a a column in my data frame filled down with a > number. > >> df$newcolumn <- number > > How can I do it? I am considering use rep() but in this case it is > necessary know the number of rows in each data base that I have and I would > like to do it in a faster ( and more elegant) way. > > > TKs > > [[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. >