โŒ

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

How to randomly select the content of some cells into a data frame?

It seems simple but I cannot find the solution: I want to randomly select some elements into a data frame imported from a .xlsx file.Is there a function such as sample_n to do this?

My problem lies in the fact that sample_n returns a number of whole rows and not single data. That is: I want a sample of elements into whatever (and possibly repeated) row.

Here is an example:

    MAS<-function(x,n){sample_n(x, n, na.rm=FALSE)}
df <- data.frame(
  "entero" = 1:4, 
  "factor" = c("a", "b", "c", "d"), 
  "numero" = c(NA, 3.4, NA, 5.6),
  "cadena" = as.character(c("a", "b", "c", "d"))
)
MAS(df,2)

which returns, for example:

      entero factor numero cadena
1      3      c     NA      c
2      4      d    5.6      d

That is, whole rows instead of single elements. I would also like to avoid the 'NA' values, by the way.

Thank you.

Resample and aggregation functions together

I have dataframe like below. Actually this is tick data from stock exchange.

                      price  quantity
date_time                            
2023-07-21 10:00:02  170.41        71
2023-07-21 10:00:20  170.68       200
2023-07-21 10:00:31  170.76        23
2023-07-21 10:00:51  170.44       139
2023-07-21 10:01:36  170.41         2
2023-07-21 10:01:17  170.48        42
2023-07-21 10:01:22  170.45         1
2023-07-21 10:01:41  170.10        10

As a result I want to get(I think step form would describe better):

  1. resample data by 5 min intervals
  2. group 'quatity' by 'price'
  3. find max 'quantity' and it`s index (some exact datetime value)
  4. create dataframe like below in table
| date_time_resampled | date_time                |price|quantity|
|2023-07-21 10:00:00  | some exact datetime value|     |        |
|2023-07-21 10:05:00  | 2023-07-21 10:00:31      |     |        |
|2023-07-21 10:10:00  | some exact datetime value|     |        |

I tried resample and groupby functions but I can`t combine them together.

โŒ
โŒ