Re: [R] problems creating data frames

From: jim holtman <jholtman_at_gmail.com>
Date: Fri, 14 Mar 2008 16:34:09 -0500

This should do what you want for the data.frame:

> x <- list(Behavior_Therapy = c(6, 7, 6, 5, 5, 5, 7, 8, 9, 6, 6, 7),

+                          Atenolol = c(4, 8, 10, 3, 6, 5, 6, 3, 7, 5, 4, 6),
+                          Placebo = c(0, 7, 0, 7, 0, 7))

> # find the longest vector and make all the others the same
> x.max <- max(sapply(x, length))
> x.pad <- lapply(x, function(z) c(z, rep(NA, x.max - length(z))))
> data.frame(x.pad)

   Behavior_Therapy Atenolol Placebo

1                 6        4       0
2                 7        8       7
3                 6       10       0
4                 5        3       7
5                 5        6       0
6                 5        5       7
7                 7        6      NA
8                 8        3      NA
9                 9        7      NA
10                6        5      NA
11                6        4      NA
12                7        6      NA

For the column names, try check.names=FALSE.

On Fri, Mar 14, 2008 at 4:01 PM, Will Holcomb <wholcomb_at_gmail.com> wrote:
> I am having two problems creating data frames that I have solutions, but
> they really seem like kludges and I assume I just don't understand the
> proper R way of doing things.
>
> The first situation is I have an set of uneven data vectors. When I try to
> use them to create a data frame I would like the bottoms of them padded with
> NAs, without explicitly specifying that. When I do:
>
> anxiety.data = data.frame(Behavior_Therapy = c(6, 7, 6, 5, 5, 5, 7, 8, 9, 6,
> 6, 7),
> Atenolol = c(4, 8, 10, 3, 6, 5, 6, 3, 7, 5, 4, 6),
> Placebo = c(0, 7, 0, 7, 0, 7))
>
> It duplicates the values for Placebo twice. I can correct this by doing:
>
> anxiety.data = data.frame(Behavior_Therapy = c(6, 7, 6, 5, 5, 5, 7, 8, 9, 6,
> 6, 7),
> Atenolol = c(4, 8, 10, 3, 6, 5, 6, 3, 7, 5, 4, 6),
> Placebo = c(c(0, 7, 0, 7, 0, 7), rep(NA, 6)))
>
> But this requires me to look at the length of the vectors and explicitly pad
> them. Is there a method to say, "create a data frame and any vectors that
> are too short should be padded with NAs"?
>
> My second situation has to do with the names of columns. When I do:
>
> rat.data = data.frame("S+/P+" = c(25,23,18,16,12,19,20,21),
> "S+/P-" = c(18,17,16,11,14,15,21,12),
> "S-/P+" = c(20,12,15,13,8,17,17,18),
> "S-/P-" = c(12,15,17,10,18,10,9,14))
>
> I end up with the column names: S..P., S..P..1, S..P..2, and S..P..3. If I
> rename them using:
>
> names(rat.data) = c("S+/P+", "S+/P-", "S-/P+", "S-/P-")
>
> Then I get them named what I attempted to name them initially. Is there some
> way to just have them named what I attempted to name them in the first
> place? I don't really know why they're being renamed since the names are
> valid, so I'm not really sure what to try to find to correct the naming.
>
> Will
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
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.
Received on Fri 14 Mar 2008 - 21:37:28 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 14 Mar 2008 - 22:30:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive