0 votes
1 view
in R Programming by (5.3k points)

I have a data frame named "mydata" that looks like this

   A  B  C   D 

1. 5  4  4   4 

2. 5  4  4   4 

3. 5  4  4   4 

4. 5  4  4   4 

5. 5  4  4   4 

6. 5  4  4   4 

7. 5  4  4   4 

I'd like to delete row 2,4,6. For example, like this:

    A  B  C   D

1. 5  4  4  4 

3. 5  4  4  4 

5. 5  4  4  4 

7. 5  4  4  4 

1 Answer

0 votes
by (25.3k points)

To delete rows in a data frame we use the (-) operator along with the row names and assign it to our data frame.

In your case:

myData <- data.frame(A= rep(5,7),

                     B= rep(4,7),

                     C= rep(4,7),

                     D = rep(4,7))

myData <- myData[-c(2,4,6),]

myData

Output:
  A B C D
1 5 4 4 4
3 5 4 4 4
5 5 4 4 4
7 5 4 4 4

Note: Make sure that you add a comma after the row numbers to include all columns.

 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...