in Education by
Can someone tell me a function which can return a specified no. of rows picked randomly without any replacement from a DataFrame in R? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
To sample random rows from a data frame, use the sample() function as follows: To create a data frame: a<- c(100,120,3,67) b <- c(4,5,NA,90) c <- c(NA,3,6,7) d <- c("A","B","C","D") data <- data.frame(col1 = a ,Col2 = b, Col3 =c, Col4 = d) head(data) Output: col1 Col2 Col3 Col4 1 100 4 NA A 2 120 5 3 B 3 3 NA 6 C 4 67 90 7 D To select random rows from the above data frame: data[sample(nrow(data), 3), ] Output: col1 Col2 Col3 Col4 3 3 NA 6 C 1 100 4 NA A 4 67 90 7 D If you want to explore more in R programming then watch this R programming tutorial for beginner:

Related questions

0 votes
    How can I sort a data.frame by multiple columns. E.x. I would like to sort the column q(descending) by column f (ascending) dd...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    I want to bind two data frames with different set of columns and I also want to retain all the columns that fails to ... do that in R? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want a way to create a empty data.frame in R, I just want to point out data types for each column and name it without any row created. df...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I have a dataframe: > myvec name order_no 1 Nehal 12 2 sejal 14 3 sejal 16 4 shyam 11 5 Nehal 12 6 Sejal 16 ... Sejal 3 Shyam 1 Ram 1 Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I want to remove some questions from data frame, I know how to delete them individually using df$x...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Can someone please tell me a quick way to convert a nested list of data whose length is 100 and each item is a list of ... 100 rows and 10 columns? I am attaching a sample data: 5...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    Here are two data frames : df1 = data.frame(CId = a(1:6), Item = b(rep("TV", 3), rep("Book", 3 ... I do a SQL style select statement Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    I want to exclude specific participants from a DataFrame and create it from an existing DataFrame. I want a method ... worked for me. Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    There are two different methods for accessing the element of a list or data frame- [ ] and [[ ]] , ... the difference between them? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    How to replace the values of NA with zeros in some column and data frame I have? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    How can I remove rows which are duplicate in a column. I have read csv file into R data.frame. For ex. ... same data in first column. Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Want to remove the lines from data frame from that : Have NAs across all columns a b c d e f 1 YASH00000206234 0 ... 0 1 2 3 2 Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    There is a DataFrame from pandas: import pandas as pd inp = [{'e2':20, 'e3':200}, {'e2':22,'e3':220}, { ... '] Can I do this in Pandas? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I want to select rows from a DataFrame based on values in some column in pandas, How can I do it? I ... WHERE column_name = some_value Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have file called q.r and it has chmod of 755, how can I run it using a command-line? sayHello...
asked Jan 24, 2022 in Education by JackTerrance
...