R-Help Community How do I add a numeric class to a data .frame. For example, I have calculated the following probabilities 1 2 3 0.0011566127 0.0009267028 0.0081623324 How would I add them back into my data.frame for example My_df <- data.frame(col_1 = c('A', 'B', 'C')) such that I end up with col_1 col_2 A 0.0011566127 Though I could use a cbind. Jeff [[alternative HTML version deleted]]
The obvious answer is My_df$col_2 <- prob_result but whether that would work might depend on the structure of prob_result. What does dput(prob_result) return? On June 4, 2023 4:11:08 PM PDT, Jeff Reichman <reichmanj at sbcglobal.net> wrote:>R-Help Community > > > >How do I add a numeric class to a data .frame. > > > >For example, I have calculated the following probabilities > > > > 1 2 3 > >0.0011566127 0.0009267028 0.0081623324 > > > >How would I add them back into my data.frame for example > > > >My_df <- data.frame(col_1 = c('A', 'B', 'C')) such that I end up with > > > >col_1 col_2 > > A 0.0011566127 > > > >Though I could use a cbind. > > > >Jeff > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org 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.-- Sent from my phone. Please excuse my brevity.
@vi@e@gross m@iii@g oii gm@ii@com
2023-Jun-05 01:59 UTC
[R] Adding a numeric class to a data.frame
Jeff R, it would be helpful if your intent was understood. For example, did you want output as a column of labels c("A", "B", "C") and another adjacent of c(0.0011566127, 0.0009267028, 0.0081623324) then you could do: data.frame(labels=c("A", "B", "C"), data=c(0.0011566127, 0.0009267028, 0.0081623324)) labels data 1 A 0.0011566127 2 B 0.0009267028 3 C 0.0081623324 If you wanted your columns labeled with the data in multiple columns, try this:> result <- data.frame(t(c(0.0011566127, 0.0009267028, 0.0081623324))) > resultX1 X2 X3 1 0.001156613 0.0009267028 0.008162332> names(result) <- c("A", "B", "C") > resultA B C 1 0.001156613 0.0009267028 0.008162332 But these are not solutions to your specified problem unless you explain properly what you want to do and the exact expected output. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Jeff Reichman Sent: Sunday, June 4, 2023 7:11 PM To: r-help at r-project.org Subject: [R] Adding a numeric class to a data.frame R-Help Community How do I add a numeric class to a data .frame. For example, I have calculated the following probabilities 1 2 3 0.0011566127 0.0009267028 0.0081623324 How would I add them back into my data.frame for example My_df <- data.frame(col_1 = c('A', 'B', 'C')) such that I end up with col_1 col_2 A 0.0011566127 Though I could use a cbind. Jeff [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org 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.