Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subsetting data.frame without column names

Tags:

r

> dput(test)
structure(list(MEMORY1 = c(7.5, 6, 6, 3.5, 5, 5), MEMORY2 = c(5, 
7.5, 3, 3.5, 5, 5), MEMORY3 = c(5, 3.5, 3, 3.5, 5, 2), MEMORY4 = c(2, 
1.5, 3, 3.5, 1, 2), MEMORY5 = c(7.5, 3.5, 3, 3.5, 5, 7), MEMORY6 = c(2, 
5, 7.5, 7.5, 5, 5), MEMORY7 = c(2, 1.5, 3, 3.5, 5, 2), MEMORY8 = c(5, 
7.5, 7.5, 7.5, 5, 8)), .Names = c("MEMORY1", "MEMORY2", "MEMORY3", 
"MEMORY4", "MEMORY5", "MEMORY6", "MEMORY7", "MEMORY8"), row.names = c(NA, 
6L), class = "data.frame")
> test
  MEMORY1 MEMORY2 MEMORY3 MEMORY4 MEMORY5 MEMORY6 MEMORY7 MEMORY8
1     7.5     5.0     5.0     2.0     7.5     2.0     2.0     5.0
2     6.0     7.5     3.5     1.5     3.5     5.0     1.5     7.5
3     6.0     3.0     3.0     3.0     3.0     7.5     3.0     7.5
4     3.5     3.5     3.5     3.5     3.5     7.5     3.5     7.5
5     5.0     5.0     5.0     1.0     5.0     5.0     5.0     5.0
6     5.0     5.0     2.0     2.0     7.0     5.0     2.0     8.0

I have a data.frame, and I would like to subset just the first row. If I do test[1, ], the result is

> test[1, ]
  MEMORY1 MEMORY2 MEMORY3 MEMORY4 MEMORY5 MEMORY6 MEMORY7 MEMORY8
1     7.5       5       5       2     7.5       2       2       5

How do I subset the data.frame so that I get just a vector of the numbers without the column names?

like image 940
Adrian Avatar asked Sep 10 '15 15:09

Adrian


People also ask

How do I exclude certain columns in R?

The most easiest way to drop columns is by using subset() function. In the code below, we are telling R to drop variables x and z. The '-' sign indicates dropping variables. Make sure the variable names would NOT be specified in quotes when using subset() function.

How do I subset a Dataframe using column names in R?

How to subset the data frame (DataFrame) by column value and name in R? By using R base df[] notation, or subset() you can easily subset the R Data Frame (data. frame) by column value or by column name.

How do I select columns based on column names in R?

To pick out single or multiple columns use the select() function. The select() function expects a dataframe as it's first input ('argument', in R language), followed by the names of the columns you want to extract with a comma between each name.


2 Answers

What you want is a numeric vector instead of a data.frame. For this, you can just use as.numeric to do the conversion

> as.numeric(df[1,])
[1] 7.5 5.0 5.0 2.0 7.5 2.0 2.0 5.0
like image 96
user295691 Avatar answered Oct 17 '22 19:10

user295691


You can use unlist with option use.names=FALSE to return only vector without names.

unlist(test[1,], use.names=FALSE)
#[1] 7.5 5.0 5.0 2.0 7.5 2.0 2.0 5.0

test[1,] is still a data.frame with 8 columns. A data.frame can be regarded as a list having the same length for its list elements (or columns). So we can use unlist. This also works when you are creating a vector from more than one row.

unlist(test[1:2,], use.names=FALSE)

Or as @Frank suggested, if we are subsetting multiple rows by keeping the dimensions, we set the names to NULL and convert to matrix.

 as.matrix(setNames(test[1:2,],NULL))
like image 20
akrun Avatar answered Oct 17 '22 18:10

akrun