1

I am giving up after trying for quite a while... I want to remove a few rows from my dataframe. I want to remove them based on the row name.

E.g.

Rowname        Age
Player 1       27
Player 2       28
Player 3       25

Now I would like to say remove row with name "Player 1" and row with name "Player 2". Because it is the rowname I am using to select it seems to be more difficult?!

Can anyone help me?

Thank you :)

Bine
  • 63
  • 2
  • 7
  • Use `dput()` to provide your data. In this case it is important to know if "Rowname" is a variable in your data or `rowname` added by R. The answer provided by Len Greski assumes that you have created a column/variable called `Rowname`. – dcarlson Jul 27 '20 at 18:50
  • @dcarlson - I used `Rowname` from the question to read the data with `read.csv()` and set that input as row names for the output data frame, but you'll see that the `data` data frame in my answer does not have a column called `Rowname`. This is why the answer uses `rownames()` to subset the data. – Len Greski Jul 27 '20 at 18:52
  • I understand. My point was that the OP did not provide what could be important information. But I got it backwards. If it is a column and not a rowname, your answer fails. – dcarlson Jul 27 '20 at 19:07

2 Answers2

2

The concept of row names in base R can have its uses; however, if you want to perform any sort of analysis with them, it's always better to make them an actual column in your data frame. Here is a replication of your data:

df <- data.frame(Age = c(27, 28, 25))
rownames(df) <- paste("Player", 1:3)
df

         Age
Player 1  27
Player 2  28
Player 3  25

This is how you can make the row names an actual part of your data. I provide two methods.

Turning row names into a data column

Method 1: base R

df$Player <- rownames(df)
rownames(df) <- NULL # This code will remove the old row names and turn them into row numbers
df

  Age   Player
1  27 Player 1
2  28 Player 2
3  25 Player 3

Method 2: the rownames_to_column() function in the tibble package

library(tibble)

rownames_to_column(df)

   rowname Age
1 Player 1  27
2 Player 2  28
3 Player 3  25

Subsetting the data frame based on the player

Now that the row names are in your data frame, you can use them to filter the data. Assuming that your data is currently:

df

   rowname Age
1 Player 1  27
2 Player 2  28
3 Player 3  25

You can do it with base R:

df[!(df$Player %in% c("Player 1", "Player 2")), ]

  Age   Player
3  25 Player 3

Or if you prefer the dplyr syntax:

library(dplyr)

df %>%
  filter(!(Player %in% c("Player 1", "Player 2")))

  Age   Player
1  25 Player 3
SavedByJESUS
  • 3,262
  • 4
  • 32
  • 47
  • Thank you. I have to issues with your methods: Method 1: My rownames dont have a name, so I cannot do "df$Player". I tried to find how to rename my rownames but no luck. Method 2: It unfortunately doesnt do anything. When I do rownames(game) I still get the same rownames as before... – Bine Jul 28 '20 at 08:33
  • "My row names don't have a (column) name, so I cannot do `df$Player"_: the code `df$Player <- rownames(df)` essentially __creates__ a new column called `"Player"` and stores the row names in it. If you do it, it will work. I suggest you copy my code and run it locally, so that study how it works. – SavedByJESUS Jul 28 '20 at 17:29
0

One approach is to use rownames() to select rows with the [ form of the extract operator as follows.

textData <- "Rownames|Age
Player 1|27
Player 2|28
Player 3|25"

data <- read.csv(text=textData,row.names="Rownames",header=TRUE,sep="|")

At this point the data data frame has three observations with one column. Each row has a row name.

# print data to show that data frame has one column, and player
# info is stored as rownames
data
> data
         Age
Player 1  27
Player 2  28
Player 3  25

Next, we'll subset the data frame.

data[!rownames(data) %in% c("Player 1","Player 2"),]

...and the output, which prints as a vector since there is only one column in the input data frame:

> data[!rownames(data) %in% c("Player 1","Player 2"),]
[1] 25

Combining this technique with subset() results in a single row data frame:

subset(data,!rownames(data) %in% c("Player 1","Player 2"))

...and the output:

> subset(data,!rownames(data) %in% c("Player 1","Player 2"))
         Age
Player 3  25
>
Len Greski
  • 10,505
  • 2
  • 22
  • 33