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

There are files file2.xlsx, It reads as follows :

Now I want to start with column seven , take file2 Replace all the characters with file1 Same , That's the seventh 、 8、 ... and 、 Nine 、 Ten columns don't need to change , because file1 and file2 The characters are consistent (3 and 1,2 and 4); From 11 Column start ,file1 and file2 The characters are different . My naming rule is from 11 Column start ,file2 Of 2 Change it to 3,4 Change 1,3 Change it to 2,1 Change it to 4;

Here is the implementation of the code :

install.packages("openxlsx") # install openxlsx Installation package 
install.packages("readxl") # install readxl Installation package
install.packages("stringr")# install 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_57hanchip
kg_ame<-as.matrix(kg_ame);kg_ame
for (i in 1:4){
g=i*2+5
j=i*2+6
if(any(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(1,2) %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]))
}
}
## The above code means that 1 Change it to 4,2 Change it to 3,3 Change it to 2,4 Change it to 1;
openxlsx::write.xlsx(kg_ame, file = "E:/myproject/kg_ame.xlsx") # Save as Excel file

R Language to achieve two files corresponding to the row and column character replacement ( Solve the problem of unification of positive and negative chains ) More articles about

  1. R Language - Explore two variables

    Purpose : By exploring documents pseudo_facebook.tsv Data to learn the analysis process of two variables Knowledge point : 1.ggplot grammar 2. How to make a scatter plot 3. How to optimize the scatter diagram 4. Conditional means 5. The correlation of variables 6. Sub distribution ...

  2. R Two arrays in a language ( Or vector ) How to calculate the outer product of

    So called array ( Or vector )a and b Outer product of , refer to a Each element and b The new elements are obtained by combining and multiplying each element of . Of course, operation rules can also be defined by themselves . The outer product operator is %o%( Be careful : The letter in the middle of the percent sign is a lowercase letter o). such as : > a ...

  3. stay R Cannot load... In the locale rJava The package solution

    Problem description : Installation package xlsx After package , function library("xlsx") The error window will pop up : RGui (64-bit): Rgui.exe - System error This program cannot be started , Because it's lost in the computer ...

  4. R Language read local file precautions

    R It should use /, instead of \ , Or two \\   R Case sensitive , So we should use C:, instead of c:

  5. R Language reading excel Of documents 3 Methods

    R Read excel Method of data in file : The computer has a excel file , The original file path is :E:\R workshop\mydata\biom excel The data is 5 ride 2 Order matrix , Element is                ...

  6. R Language reading EXCEL Various methods of documentation

    routing problem Original file path C:\Users\air\Desktop\1.txt R There are two ways to read the path C:\\Users\\air\\Desktop\\1.txt C:/Users/air/Deskt ...

  7. R Language folder operation examples ( turn )

    rm(list=ls())path = 'J:/lab/EX29 -- stay R Language in the file ( clip ) operation 'setwd(path)cat("file A\n", file="A& ...

  8. Use R Language - For matrix ( form ) The name of the line

    from :http://www.dataguru.cn/article-2217-1.html R Matrices are often used in languages ( form ) Data processing , In complex data , It becomes particularly important to define a name for its ranks . Processing huge amounts of data ...

  9. R A quick introduction to language

    R Language is a comprehensive open source language for statistical analysis and data science ,R The official website of :http://www.r-project.org/  stay Windows Installation in environment R It's very convenient R Two modes of language operation : Interactive mode and batch mode ...

Random recommendation

  1. Anti-If: The missing patterns-- turn

    Original address :http://code.joejag.com/2016/anti-if-the-missing-patterns.html Around 10 years ago I encountered ...

  2. SystemTap knowledge ( Two )

    Unbuntu install systemtap: http://www.cnblogs.com/hdflzh/archive/2012/07/25/2608910.html 1 Update source to http://mirro ...

  3. Asynchronous and synchronous http Request timeout mechanism

    Asynchronous timeout settings : Example : Example: do a simple HTTP GET request for http://www.nethype.de/ and print the response ...

  4. Yes JSON The best field in is 【 character 】 Instead of 【enum】 The thought of

    Recently, I heard a quote from a bull man : 1, If the protocol defines tag Words , The resolution of the protocol does not depend on change , So development is more independent . eg: good: name=“zl”, gender=“f” bad: name=" ...

  5. [ turn ]Converting a C library to gyp

    Source:http://n8.io/converting-a-c-library-to-gyp/ Converting a C library to gyp Published: 2012.10. ...

  6. Alipay wap Payment calls client

    https://mclient.alipay.com/home/exterfaceAssign.htm?alipay_exterface_invoke_assign_client_ip=183.15. ...

  7. Smobiler Service What is it? ?(Smobiler——.NET Mobile development platform )

    Learn from Smobiler New products coming soon SmobilerService after , The first question many people have is ——Smobiler Service What is it? ? Smobiler The developers of this exe The window must not be strange , Sometimes because ...

  8. python3 Common ways of Dictionary

    Dictionary method ( It may need to be rearranged ) function explain D Represents the dictionary object   D.clear() Empty dictionary D.pop(key) Remove key , At the same time, return the value corresponding to this key D.copy() Return dictionary D Copy of , Just copy one layer ( Light copy ...

  9. obtain iframe Internal elements

    $("#iframeID").contents().find("#index_p") 2 Get the value of the parent form $('#father', parent.document ...

  10. Dubbo Fault tolerance and load balancing

    Although I have just introduced dubbo I wrote about it in , But not enough , Let's write about it in detail , In future research , We will continue to supplement the principle of procedure . One : Fault tolerance 1. Mechanism When the cluster call fails ,Dubbo A variety of fault tolerance schemes are provided , Default is failove ...