Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove one row from dataframe in R

Tags:

dataframe

r

I'm totally confused ! I have a one column dataframe in R :

temp1 = structure(list(Hamburg = c("Hamburg", "4562", "4604")), class = "data.frame", row.names = c(NA, 
-3L))

str(temp1)
'data.frame':   3 obs. of  1 variable:
 $ Hamburg: chr  "Hamburg" "4562" "4604"

When I remove the first row by :

temp1 = temp1[-1,]

then the remaining is not a dataframe any more ! and I do not have the column name as well !

temp1
[1] "4562" "4604"

str(temp1)
 chr [1:2] "4562" "4604"

How could I fix it ? I would like to keep the dataframe structure just get rid of the first row !

like image 909
user9112767 Avatar asked May 01 '20 16:05

user9112767


People also ask

How do I remove one row from data in R?

R provides a subset() function to delete or drop a single row and multiple rows from the DataFrame (data. frame), you can also use the notation [] and -c().

How do you omit a row in R?

To remove rows with an in R we can use the na. omit() and <code>drop_na()</code> (tidyr) functions. For example, na.

How do I delete a row in R studio?

You cannot actually delete a row, but you can access a data frame without some rows specified by negative index. This process is also called subsetting in R language. A Big Note: You should provide a comma after the negative index vector -c().


1 Answers

temp1 = temp1[-1,, drop=F]
str(temp1)
'data.frame':   2 obs. of  1 variable:
 $ Hamburg: chr  "4562" "4604"

The default is T, which reduces the data.frame to its smallest dimension How do I extract a single column from a data.frame as a data.frame?

like image 99
desval Avatar answered Oct 23 '22 03:10

desval