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]]
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?
This was just discussed this week: https://stat.ethz.ch/pipermail/r-help/2008-March/157082.html On Fri, Mar 14, 2008 at 5: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. >
Maybe Matching Threads
- lines(..., lwd=3) inaccuracy
- How to best deal with undesirable Induction Variable Simplification?
- Pseudo-instruction that overwrites its input register
- Asking Favor For the Script of Median Filter
- How to best deal with undesirable Induction Variable Simplification?