In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What this article shares with you is an example analysis of filtering according to the order of data boxes in R language. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
Purpose
There are two data boxes, both of which have the same column (ID). Here you want to extract the first data box according to the ID column of the second data box, in the same order as the second data box.
Data Box 1 > tt = data.frame (id = 1GRO 10 ~ y = rnorm (10))
> tt
Id y
1 1 0.7264999
2 2-1.3817018
3 3-0.8626703
4 4 2.0663756
5 5 0.1997253
6 6 0.5968497
7 7-0.8836847
8 8 2.2224643
9 9-1.5825250
10 10-0.1530456
Data Box 2 > id = data.frame (id = c (2, 1, 5, 4, 3))
> id
Id
1 2
2 1
3 5
4 4
5 3
Wrong method: extract with% in%, it will be sorted automatically > # when matching with% in%, it will be sorted automatically, not in the order of id
> tt [tt$id% in% id$id,]
Id y
1 1 0.7264999
2 2-1.3817018
3 3-0.8626703
4 4 2.0663756
5 5 0.1997253
> id
Id
1 2
2 1
3 5
4 4
5 3
As you can see, the order of matching is 1, 2, 3, 4, 5, instead of the original, 2, 1, 5, 5, 4, 5, 4, 5, 4, 5, 4, 4, 5, 4, 4, 4, 5, 4, 4, 5, 4, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5
The right way: record the location with match, and then extract it according to the location > # use match to achieve the goal
> loc = match (id$id,tt$id)
> loc
[1] 2 1 5 4 3
> tt [loc,]
Id y
2 2-1.3817018
1 1 0.7264999
5 5 0.1997253
4 4 2.0663756
3 3-0.8626703
Conclusion: match really smells good.
Complete Code:
# simulate two data boxes
Tt = data.frame (id = 1RU 10J y = rnorm (10))
Tt
Id = data.frame (id = c (2pm, 5pm, 4pm))
Id
# when using% in% for matching, it will be sorted automatically, not in the order of id
Tt [tt$id% in% id$id,]
Id
# you can use match to achieve your goal
Loc = match (id$id,tt$id)
Loc
Tt [loc,]
My rollover record.
Originally, I had two genealogical documents. The first one had many genealogical documents, but there were some mistakes. The second pedigree file is a subset of the first pedigree file, and its pedigree is correct. I want to correct the genealogy error in the first genealogy file.
"my thinking:"
1. Use% in% to extract the ID of the first pedigree according to the ID of the second pedigree, and then replace the Sire and Dam of the corresponding IID of the first pedigree with the Sire and Dam of the second pedigree. If the second pedigree itself is sorted, then this is fine.
"potential bug"
If the second pedigree is not arranged in order, there will be an error in the above operation. For example, if the order after matching is (1), (1), (1), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (1), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2), (2)
The above is an example analysis of filtering according to the order of data boxes in R language. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.