[R] How to replace range of data in a dataframe

jim holtman jholtman at gmail.com
Wed Feb 15 11:03:28 CET 2012


Is this what you want:

> x <- read.table(text = "ColumnA   ColumnB   ColumnC
+  a         d         d
+  s         s         e
+  e         r         e
+  y         w         y
+  t         u         i
+  q         e         l", as.is = TRUE, header = TRUE)
>  x$ColumnC[1:3] <- runif(3)
> x
  ColumnA ColumnB           ColumnC
1       a       d 0.965288798790425
2       s       s 0.722888228250667
3       e       r 0.634451690129936
4       y       w                 y
5       t       u                 i
6       q       e                 l
>

Now ColumnC is character, so the random numbers are character strings.
 What is the problem you are trying to solve?

On Wed, Feb 15, 2012 at 4:38 AM, Camilo Mora <cmora at dal.ca> wrote:
> Hi:
> I wonder if anyone may know the answer to this question:
>
> COnsider the following data frame (DF):
>
> ColumnA   ColumnB   ColumnC
>  a         d         d
>  s         s         e
>  e         r         e
>  y         w         y
>  t         u         i
>  q         e         l
>
> What I need is to replace rows 1 to 3 in ColunmC for a random number?
> I tried DF$ColumnC[DF$ColumnC [1:3]] <- .Random.seed[1:3]
> but does not work.
>
> It may be trivial but have not been able to find a solution.
>
> Thanks,
> Camilo
>
> ______________________________________________
> 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.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.



More information about the R-help mailing list