How to extract top row of a vector variable

Viewed 33

I have this variable filtered that contains what appears to be a list with to rows. I have not been able to determine it's type, or how to extract from it only the values I want to keep.

C17S35076296E35282086 C12S84542006E85443011 C17S41062669E41447005 C12S85450052E85962613 C12S70574871E71644041 
         3.390521e-23          1.532544e-06          2.463260e-06          5.123223e-06          6.373701e-06 

I need to keep only the top row of this variable, so I need to drop the numbers. This vector was populated a follows:

p_values <- c()
for (i in names(data)[-1]) {
    # Create a contingency table
    cont_tbl <- table(data[[i]], df_classes[[1]])
    # Perform Fisher's exact test
    test_result <- fisher.test(cont_tbl)
    # Add the p-value of the test to the list
    p_values[i] <- test_result$p.value
} 

filtered <- sort(p_values)[1:20]

I tried the following

filtered[1]
filtered[,1]
filtered[1,]
filtered[1][1]

All to no avail.

2 Answers

This is a named numeric vector, see example:

# example data
x <- setNames(1:3, letters[1:3])

str(x)
# Named int [1:3] 1 2 3
# - attr(*, "names")= chr [1:3] "a" "b" "c"

x
# a b c 
# 1 2 3 

names(x)
# [1] "a" "b" "c"

unname(x)
# [1] 1 2 3

Did you try ?

names(filtered)[1]

For instance :

v <- c(1, 2, 3)
names(v) <- c("a", "b", "c")
str(v)

It gives you :

 Named num [1:3] 1 2 3
 - attr(*, "names")= chr [1:3] "a" "b" "c"
Related