Problems to create a database

Hello,

I have to create a data base,
for that i have my values and their occurences in a dataframe made of 2 columns..
For instance i have the figure 1.44 and i know that it appears 10 times; then i have the figure 1.95 and i know that it appears 14 times.. So i have to create a database where they will be 1.44 appearing ten times, 1.95 appearing 14 times... But i have no idea of how i can do that ..

Thank you for your help :slight_smile:

this is how my dataframe looks like for now :

The uncount() function from tidyr can do this.

library(tidyr)
DF <- data.frame(Size = c(0.6, 1.44, 1.95), Occurence = c(0, 10, 14))
DF
#>   Size Occurence
#> 1 0.60         0
#> 2 1.44        10
#> 3 1.95        14
NewDF <- uncount(DF, weights = Occurence)
NewDF
#>    Size
#> 1  1.44
#> 2  1.44
#> 3  1.44
#> 4  1.44
#> 5  1.44
#> 6  1.44
#> 7  1.44
#> 8  1.44
#> 9  1.44
#> 10 1.44
#> 11 1.95
#> 12 1.95
#> 13 1.95
#> 14 1.95
#> 15 1.95
#> 16 1.95
#> 17 1.95
#> 18 1.95
#> 19 1.95
#> 20 1.95
#> 21 1.95
#> 22 1.95
#> 23 1.95
#> 24 1.95

Created on 2022-05-02 by the reprex package (v2.0.1)

Thank you really much!

And do you know (to go faster) if we can directly name our columns to create the dataframe :
DF <- data.frame(Size = c(column A), Occurence = c(column B)) ?

And a last question, if i have a third column the code would be like :
DF <- data.frame(Size = c(column A), Occurence = c(column B); c(column A), Occurence = c(column C)) ?

Thank you :slight_smile:

You don't need to do that, that part of the code is just to provide some made up sample data to work with for reproducibility purposes, you can simply use your own data frame instead. The key to solve your problem is the uncount() function.

To better understand why the solution given to you includes code to create a dataframe, please read the guide on this link

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.