Analysis in R: Examples of commands for extracting data about vectors

RAnalytics

This is a sample command for extracting data from vectors. This is a rudimentary introduction to the topic.

Checked with R version 4.2.2.

スポンサーリンク
Sponsored Link

Example

See the command and package help for details.

###Creating Data#####
TestData <- 1:10
#Check
TestData
[1]  1  2  3  4  5  6  7  8  9 10
########

#Extraction of the fifth element
TestData[5]
[1] 5

#Replace the 5th element with 0
TestData[5] <- 0
#Check
TestData
[1]  1  2  3  4  0  6  7  8  9 10

#Extract even numbers from data
#But, 0 is excluded
TestData[TestData %% 2 == 0 & TestData != 0]
[1]  2  4  6  8 10

#Assign a name to the data
names(TestData) <- letters[1:10]
#Check
TestData
a  b  c  d  e  f  g  h  i  j 
1  2  3  4  0  6  7  8  9 10

#Extract data by name
TestData["e"]
e 
0

#Extract by range
TestData[4:9]
d e f g h i 
4 0 6 7 8 9 

I hope this makes your analysis a little easier !!

タイトルとURLをコピーしました