R函数切换列名

huangapple go评论63阅读模式
英文:

R function to switch column names

问题

reproducible minimal example dataset

pid<-1:30
y1<-rnorm(30,10,10)
y2<-rnorm(30,10,10)
x1<-rnorm(30,5,2)
x2<-rnorm(30,5,2)

data<-data.frame(pid,y1,y2,x1,x2)

This is small reproducible example, but I have more variables such as I1, I2, etc...

I am trying to make a function that can switch the function name so that 'y1' become 'y2'
and 'y2' become 'y1'
'x1' become 'x2' and 'x2' become 'x1'


rename<-function(data=data,
                 
                 dv=y,
                 IV=x,
                 number_of_IV=1){

   
rename_with(
  data,
  .fn=~gsub(
    pattern='1^',
    replacement='2',
    .x
  ),
  .cols=matches('^y'|'^x')
)
  
rename_with(
  data,
  .fn=~gsub(
    pattern='2^',
    replacement='1',
    .x
  )
  .cols=matches('^y'|'^x')
  )
)
 
}


However, it does not work.


trouble shooting the fuction that I wrote, so that I can use that function. 
英文:

reproducible minimal example dataset

pid&lt;-1:30
y1&lt;-rnorm(30,10,10)
y2&lt;-rnorm(30,10,10)
x1&lt;-rnorm(30,5,2)
x2&lt;-rnorm(30,5,2)

data&lt;-data.frame(pid,y1,y2,x1,x2)

This is small reproducible example, but I have more variables such as I1, I2, etc...

I am trying to make a function that can switch the function name so that 'y1' become 'y2'
and 'y2'become 'y1'
'x1' become 'x2' and 'x2' become 'x1'


rename&lt;-function(data=data,
                 
                 dv=y,
                 IV=x,
                 number_of_IV=1){

   
rename_with(
  data,
  .fn=~gsub(
    pattern=&#39;1^&#39;,
    replacement=&#39;2&#39;,
    .x
  ),
  .cols=matches(&#39;^y&#39;|&#39;^x&#39;)
)
  
rename_with(
  data,
  .fn=~gsub(
    pattern=&#39;2^&#39;,
    replacement=&#39;1&#39;,
    .x
  )
  .cols=matches(&#39;^y&#39;|&#39;^x&#39;)
  )
)
 
}


However, it does not work.


trouble shooting the fuction that I wrote, so that I can use that function. 

答案1

得分: 1

以下是翻译好的部分:

为什么你想要这个?似乎是一个 xy问题

无论如何,以下是一种方法来实现它

head(data)
  pid       x1        y1       x2         y2
1   1 5.087567  4.424669 6.830336 11.6921347
2   2 4.386710 21.870220 1.898388 21.0484717
3   3 5.154918  4.103774 7.926982  4.3101622
4   4 2.528215  3.572182 5.407407  8.3941766
5   5 6.003412 13.877344 6.188354  0.1214239
6   6 8.527333  6.971523 4.559890  7.3657691

head(data) %>%
   rename_with(~str_replace_all(.,&quot;\\d$&quot;, ~abs(2-as.numeric(.))+1))

  pid       x2        y2       x1         y1
1   1 5.087567  4.424669 6.830336 11.6921347
2   2 4.386710 21.870220 1.898388 21.0484717
3   3 5.154918  4.103774 7.926982  4.3101622
4   4 2.528215  3.572182 5.407407  8.3941766
5   5 6.003412 13.877344 6.188354  0.1214239
6   6 8.527333  6.971523 4.559890  7.3657691
英文:

Why would you want this? Seems like an xy problem

Anyway here is one way to do it

head(data)
  pid       x1        y1       x2         y2
1   1 5.087567  4.424669 6.830336 11.6921347
2   2 4.386710 21.870220 1.898388 21.0484717
3   3 5.154918  4.103774 7.926982  4.3101622
4   4 2.528215  3.572182 5.407407  8.3941766
5   5 6.003412 13.877344 6.188354  0.1214239
6   6 8.527333  6.971523 4.559890  7.3657691

head(data) %&gt;%
   rename_with(~str_replace_all(.,&quot;\\d$&quot;, ~abs(2-as.numeric(.))+1))

  pid       x2        y2       x1         y1
1   1 5.087567  4.424669 6.830336 11.6921347
2   2 4.386710 21.870220 1.898388 21.0484717
3   3 5.154918  4.103774 7.926982  4.3101622
4   4 2.528215  3.572182 5.407407  8.3941766
5   5 6.003412 13.877344 6.188354  0.1214239
6   6 8.527333  6.971523 4.559890  7.3657691

答案2

得分: 0

你可以使用 chartr1 更改为 2,将 2 更改为 1

head(data, 2)
#  pid       y1        y2       x1       x2
#1   1 31.54238 -3.972442 5.178496 4.370217
#2   2 22.21881 11.122397 4.384974 4.177646

names(data) <- chartr("12", "21", names(data))

head(data, 2)
#  pid       y2        y1       x2       x1
#1   1 31.54238 -3.972442 5.178496 4.370217
#2   2 22.21881 11.122397 4.384974 4.177646
英文:

You can use chartr to change 1 to 2 and 2 to 1.

head(data, 2)
#  pid       y1        y2       x1       x2
#1   1 31.54238 -3.972442 5.178496 4.370217
#2   2 22.21881 11.122397 4.384974 4.177646

names(data) &lt;- chartr(&quot;12&quot;, &quot;21&quot;, names(data))

head(data, 2)
#  pid       y2        y1       x2       x1
#1   1 31.54238 -3.972442 5.178496 4.370217
#2   2 22.21881 11.122397 4.384974 4.177646


</details>



huangapple
  • 本文由 发表于 2023年4月1日 00:06:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定