英文:
Updating the names in dataframe to keep the same always
问题
在绑定所有小表时,数据框的名称具有扩展名“.x”、“ .y”、“ .xX”、“ .yY”等,但我希望在数据框中保持相同的名称。
例如,在我的数据框中,我希望保持相同的列名“names”。
data <- data.frame(part=c("DD","MM","TT"),
Team1 = c("kamal", "vimal", "shamal"),
names = c(45,23,35),
Team2 = c("kamal", "vimal", "shamal"),
names = c(46,24,36),
Team3 = c("kamal", "vimal", "shamal"),
names = c(47,25,37),
Team2 = c("kamal", "vimal", "shamal"),
names = c(48,26,38),
age = c(45,23,35),
weight = c(50,34,42))
所需输出应该如下所示。
英文:
While rbinding the all small tables, names of data frame having extentention ".x" , ".y" , ".xX", ".yY" etc. but I want to keep the same name in dataframe.
For example in my data frame below I want to keep the same column name "names" in data frame.
data <- data.frame(part=c("DD","MM","TT"),
Team1 = c("kamal", "vimal", "shamal"),
"names.x" = c(45,23,35),
Team2 = c("kamal", "vimal", "shamal"),
"names.y" = c(46,24,36),
Team3 = c("kamal", "vimal", "shamal"),
"names.x.x" = c(47,25,37),
Team2 = c("kamal", "vimal", "shamal"),
"names.y.y" = c(48,26,38),
age = c(45,23,35),
weight = c(50,34,42))
Required output should be look like below
答案1
得分: 1
使用`sub`函数只保留“names”,然后使用`setNames`函数更改列名。
setNames(data, sub("(names).*", "\1", colnames(data)))
part Team1 names Team2 names Team3 names Team2.1 names age weight
1 DD kamal 45 kamal 46 kamal 47 kamal 48 45 50
2 MM vimal 23 vimal 24 vimal 25 vimal 26 23 34
3 TT shamal 35 shamal 36 shamal 37 shamal 38 35 42
<hr>
或者移除点号后的内容。
setNames(data, sub("\..*", "", colnames(data)))
另一种方法是使用`colnames(data) <-`语法,它将立即替换`data`中的列名(请谨慎使用)。
colnames(data) <- sub("\..*", "", colnames(data))
### 输出
part Team1 names Team2 names Team3 names Team2 names age weight
1 DD kamal 45 kamal 46 kamal 47 kamal 48 45 50
2 MM vimal 23 vimal 24 vimal 25 vimal 26 23 34
3 TT shamal 35 shamal 36 shamal 37 shamal 38 35 42
<hr>
### 更新:
要将`names`与顺序数字组合,可以使用以下方法:
colnames(data)[grepl("names", colnames(data))] <- paste0("names", seq_len(sum(grepl("names", colnames(data)))))
part Team1 names1 Team2 names2 Team3 names3 Team2.1 names4 age weight
1 DD kamal 45 kamal 46 kamal 47 kamal 48 45 50
2 MM vimal 23 vimal 24 vimal 25 vimal 26 23 34
3 TT shamal 35 shamal 36 shamal 37 shamal 38 35 42
<details>
<summary>英文:</summary>
You can use `sub` to only keep "names", then `setNames` to change the column names.
setNames(data, sub("(names).*", "\1", colnames(data)))
part Team1 names Team2 names Team3 names Team2.1 names age weight
1 DD kamal 45 kamal 46 kamal 47 kamal 48 45 50
2 MM vimal 23 vimal 24 vimal 25 vimal 26 23 34
3 TT shamal 35 shamal 36 shamal 37 shamal 38 35 42
<hr>
Or remove anything after a dot `.`.
setNames(data, sub("\..*", "", colnames(data)))
Another way of doing it is to use the `colnames(data) <-` syntax, which will immediately replace the column names in `data` (use with caution).
colnames(data) <- sub("\..*", "", colnames(data))
### Output
part Team1 names Team2 names Team3 names Team2 names age weight
1 DD kamal 45 kamal 46 kamal 47 kamal 48 45 50
2 MM vimal 23 vimal 24 vimal 25 vimal 26 23 34
3 TT shamal 35 shamal 36 shamal 37 shamal 38 35 42
<hr>
### Update:
To combine `names` with sequential numbers, do:
colnames(data)[grepl("names", colnames(data))] <- paste0("names", seq_len(sum(grepl("names", colnames(data)))))
part Team1 names1 Team2 names2 Team3 names3 Team2.1 names4 age weight
1 DD kamal 45 kamal 46 kamal 47 kamal 48 45 50
2 MM vimal 23 vimal 24 vimal 25 vimal 26 23 34
3 TT shamal 35 shamal 36 shamal 37 shamal 38 35 42
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论