[R] Adding a numeric class to a data.frame

@vi@e@gross m@iii@g oii gm@ii@com @vi@e@gross m@iii@g oii gm@ii@com
Tue Jun 6 02:55:07 CEST 2023


Jeff,

I wish I could give you an answer to a very specific question.

You have lots of numbers in a vector representing whatever "probabilities"
mean something to you. There are currently no names associated with them.
And you want to make some kind of graph using ggplot.

So, to be quite clear, ggplot tends to like a data.frame or one of several
other such tabular constructs when making graphs, or have some data coerced
into such a format. BUT I am aghast at the concept of giving it a data.frame
with one row and thousands of un-named columns. First, the columns will have
semi-numerical names by default and second, they cannot be used by ggplot
unless you specify a name.

What you normally need is not lots of columns but lots of rows. One column
suffices for some purposes and multiple columns are often present for many
purposes. 

But what are you graphing as in probability versus what? Is that item
correlated with each result in some way? 

You eventually need to probably make a data.frame with two or more such
columns with names for the columns. You need to tell ggplot something like 

ggplot(mydata, aes(x=whatever, y=whatever, ...)) + geom_line(or whatever)
...

But as you release info this slowly, I think I will now drop out of this
conversation.

Good luck.

-----Original Message-----
From: Jeff Reichman <reichmanj using sbcglobal.net> 
Sent: Monday, June 5, 2023 7:29 AM
To: avi.e.gross using gmail.com; r-help using r-project.org
Subject: RE: [R] Adding a numeric class to a data.frame

Avi

But I don't have a column header to call. Do I simply use column position 

> pred_probability 
             1              2              3              5              8 
0.001156612672 0.000926702837 0.008162332353 0.001544764162 0.000919503109
..........
> str(pred_probability )
 Named num [1:6964] 0.001157 0.000927 0.008162 0.001545 0.00092 ...
 - attr(*, "names")= chr [1:6964] "1" "2" "3" "5" ...
>

Jeff

-----Original Message-----
From: avi.e.gross using gmail.com <avi.e.gross using gmail.com> 
Sent: Sunday, June 4, 2023 9:58 PM
To: 'Jeff Reichman' <reichmanj using sbcglobal.net>; r-help using r-project.org
Subject: RE: [R] Adding a numeric class to a data.frame

Jeff,

The number of items is not relevant except insofar as your vector of
probabilities is in the same order as the other vector and the same length.

If for example you had a vector of test scores for 10,000 tests and you
calculated the probability in the data of having a 100, then the probability
of a 99 and so on, then you could make another vector of 10,000 giving the
probability of the corresponding entries.

So before calling ggplot, assuming you have two vectors called orig and
prob, you make a data.frame like

Df <- data.frame(orig=orig, prob=prob)

You use that in ggplot.

You can of course add additional columns. Or if your data is in another
format, do things like long to wide conversion and many other things.

If you already have a data.frame with one or more columns including orig,
you can attache the probabilities with something as simple as:

Df$prob = prob

If you are using ggplot, you may as well be using elements of the tidyverse
that provide a different take on how to do some things compared to base R
but that is not something easily discussed here.



-----Original Message-----
From: Jeff Reichman <reichmanj using sbcglobal.net>
Sent: Sunday, June 4, 2023 10:21 PM
To: avi.e.gross using gmail.com; r-help using r-project.org
Subject: RE: [R] Adding a numeric class to a data.frame

Yes - I could have done that but I have over 5,000 calculated probabilities.
So yes a little more detail would have helped. I'm needing to add those
probability back into the original data.frame from which the model was
created as I'm going  to be using ggplot2 so I need the probabilities and
original dataframe to be one.

-----Original Message-----
From: avi.e.gross using gmail.com <avi.e.gross using gmail.com>
Sent: Sunday, June 4, 2023 9:00 PM
To: 'Jeff Reichman' <reichmanj using sbcglobal.net>; r-help using r-project.org
Subject: RE: [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))) 
> result
           X1           X2          X3
1 0.001156613 0.0009267028 0.008162332
> names(result) <- c("A", "B", "C")
> result
            A            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 using r-project.org> On Behalf Of Jeff Reichman
Sent: Sunday, June 4, 2023 7:11 PM
To: r-help using 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 using 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.



More information about the R-help mailing list