更新数据框中的名称以始终保持相同。

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

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 &lt;- data.frame(part=c(&quot;DD&quot;,&quot;MM&quot;,&quot;TT&quot;),
Team1 = c(&quot;kamal&quot;, &quot;vimal&quot;, &quot;shamal&quot;),
&quot;names.x&quot; = c(45,23,35),
Team2 = c(&quot;kamal&quot;, &quot;vimal&quot;, &quot;shamal&quot;),
&quot;names.y&quot; = c(46,24,36),
Team3 = c(&quot;kamal&quot;, &quot;vimal&quot;, &quot;shamal&quot;),
&quot;names.x.x&quot; = c(47,25,37),
Team2 = c(&quot;kamal&quot;, &quot;vimal&quot;, &quot;shamal&quot;),
&quot;names.y.y&quot; = 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


&lt;hr&gt;

或者移除点号后的内容。

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


&lt;hr&gt;

### 更新:

要将`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 &quot;names&quot;, 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


&lt;hr&gt;

Or remove anything after a dot `.`.

setNames(data, sub("\..*", "", colnames(data)))


Another way of doing it is to use the `colnames(data) &lt;-` 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


&lt;hr&gt;

### 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>



huangapple
  • 本文由 发表于 2023年3月7日 19:49:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661613.html
匿名

发表评论

匿名网友

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

确定