0

I noticed that thought using dplyr is quite handy when handling dataframe, sometimes when using select or slice, the resulting data.frame cannot be converted to numeric, in order to do subsequent analysis, such as linear regression.

Here is my simple codes, where I want to select the first 100 rows:

Y <- data.frame(rnorm(1000,0,1))
Y_used <- Y %>% slice_head( n = 100)

The vector Y_used is of data.frame, and the function as.numeric() seems not working.

If I still want to use slice to select it, how can I convert Y_used to numeric? Thanks!

tobinz
  • 305
  • 1
  • 7
  • 1
    If you want single column as numeric you can `pull` the data instead. `Y %>% pull(col_name)`. – Ronak Shah Oct 15 '20 at 12:50
  • @RonakShah I think ```pull``` is used to select columns. Here I wanna select the first 100 rows of a vector. – tobinz Oct 15 '20 at 12:56
  • Please provide an example of your data along with expected output. – Ronak Shah Oct 15 '20 at 12:58
  • @RonakShah Yes, I have an example. – tobinz Oct 15 '20 at 13:16
  • You have not shown your expected output. Also you didn't name the column in `df`. So if your dataframe is `Y <- data.frame(a = rnorm(1000,0,1))`. Is `Y %>% slice_head( n = 100) %>% pull(a)` what you want? – Ronak Shah Oct 15 '20 at 15:11

0 Answers0