Fill down a new column in data frame with a number

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Fill down a new column in data frame with a number

Marcelo Mariano Silva
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Fill down a new column in data frame with a number

Sarah Goslee
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
<[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Fill down a new column in data frame with a number

S Ellison-2
In reply to this post by Marcelo Mariano Silva
> 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}}

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Fill down a new column in data frame with a number

Rui Barradas
In reply to this post by Marcelo Mariano Silva
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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.