英文:
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<-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.
答案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(.,"\\d$", ~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) %>%
rename_with(~str_replace_all(.,"\\d$", ~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
你可以使用 chartr
将 1
更改为 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) <- 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
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论