[R] Fill down a new column in data frame with a number

Sarah Goslee @@r@h@go@|ee @end|ng |rom gm@||@com
Thu May 10 15:14:29 CEST 2018


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))
> mydf
  a          b
1 a 0.52880425
2 b 0.02422788
3 c 0.56089766
4 d 0.99007900
5 e 0.45031033
> mydf$newcolumn <- 10
> mydf
  a          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 using 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




More information about the R-help mailing list