Retrieve columns that do not have a header or name in R

I need to extract columns from a data set without headings.

I have a ~ 10,000 x 3 dataset and I need to build the first column for the second second.

I know how to do this when the columns have the names ~ plot(data$V1, data$V2) , but in this case they do not. How can I access each column separately if they have no names?

thanks

+4
source share
2 answers

Why not give them reasonable names?

 names(data)=c("This","That","Other") plot(data$This,data$That) 

This is a better solution than using a column number, as the names make sense and if your data changes to have a different number of columns, your code may break in several places. Give your data the correct names and as long as you always reference data$This , then your code will work.

+5
source

I usually select columns by their position in the matrix / data frame.

eg.

dataset[,4] to select the 4th column.

The first number in brackets refers to the rows, the second to columns. I did not use the β€œ1st number” here, so all the rows in column 4 are selected, i.e. Entire column.

This is easy to remember because it is related to matrix computing. For example, a 4 Γ— 3 matrix has 4 rows and 3 columns. So when I want to select the first row of the third column, I could do something like matrix[1,3]

+6
source

Source: https://habr.com/ru/post/1484121/


All Articles