R language implementation two file corresponding column character substitution

Source: Internet
Author: User

Suppose there is a file file1.xlsx, which reads as follows:

There is a file file2.xlsx, which reads as follows:

Now I want to start with column seventh and replace all file2 characters with file1, i.e. the 第七、八、九、十 column does not need to change because the characters of file1 and File2 are the same (3 and 4), and file1 and file2 characters are different from column 11th. My naming rules are starting from column 11th, File2 2 changed to 3,4 to 1,3 to 2,1 to 4;

Here is the code implementation process:

Install.packages ("Openxlsx") #安装openxlsx安装包install. Packages ("READXL") #安装readxl安装包install. Packages ("Stringr") # Install the Stringr installation package library ("Stringr") library (READXL) library (openxlsx) model_57hanchip<-read_excel ("e:/myproject/ File1.xlsx ") kg_ame<-read_excel (" e:/myproject/file2.xlsx ") Model_57hanchip<-as.matrix (model_57hanchip); Model_57hanchipkg_ame<-as.matrix (Kg_ame); Kg_amefor (i in 1:4) {g=i*2+5 j=i*2+6 if (Intersect (model_57hanchip[, G:J], kg_ame[,g:j]) >0) ==true | All (kg_ame[,g:j]==0) ==true) {print (C (g,j))}else if (All (c ()%in% kg_ame[,g:j]) ==true) {Kg_ame[,g:j] =str_replace    _all (Kg_ame[,g:j], "1", "4");    KG_AME[,G:J] =str_replace_all (Kg_ame[,g:j], "2", "3"); Print (c (kg_ame[,g],kg_ame[,j)))}else if (All (c (1,3)%in% kg_ame[,g:j]) ==true) {Kg_ame[,g:j] =str_replace_all (kg_ame[    , G:j], "1", "4");    KG_AME[,G:J] =str_replace_all (Kg_ame[,g:j], "3", "2"); Print (c (kg_ame[,g],kg_ame[,j))}else if (All (c (2,4)%in% kg_ame[,g:j]) ==true) {Kg_ame[,g:j] =str_replace_All (Kg_ame[,g:j], "2", "3");    KG_AME[,G:J] =str_replace_all (Kg_ame[,g:j], "4", "1");    Print (c (kg_ame[,g],kg_ame[,j))}else {kg_ame[,g:j] =str_replace_all (Kg_ame[,g:j], "3", "2");    KG_AME[,G:J] =str_replace_all (Kg_ame[,g:j], "4", "1"); Print (c (kg_ame[,g],kg_ame[,j)))}}# #上述的代码的意思是将1改为4, 2 instead of 3,3 to 2,4 instead of 1;openxlsx::write.xlsx (kg_ame, file = "e:/ Myproject/kg_ame.xlsx ") #保存为Excel文件

  

R language implementation two file corresponding column character substitution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.