Get all first four digit values within a column of data and make those values a new column of data in R

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

Get all first four digit values within a column of data and make those values a new column of data in R

问题

I'm having trouble separating values within a column. Let's assume we have:

Column A Column B Column C
1 A 123456
2 B 789101
3 C 112131
4 D 415161

I'm trying to get the first four numbers from all the values from Column C and get all the new values into a new within the dataset like so:

Column A Column B Column C Column D
1 A 123456 1234
2 B 789101 7891
3 C 112131 1121
4 D 415161 4151

How would I go about doing this in R

I've tried using:

df$columnd = substr(columnc, 1, 4)

This doesn't seem to work. The other thing is that I want it not to be attached to the same dataset. I want it all to be attached to a different variable. I'm not sure if that made any sense.

英文:

I'm having trouble separating values within a column. Let's assume we have:

Column A Column B Column C
1 A 123456
2 B 789101
3 C 112131
4 D 415161

I'm trying to get the first four numbers from all the values from Column C and get all the new values into a new within the dataset like so:

Column A Column B Column C Column D
1 A 123456 1234
2 B 789101 7891
3 C 112131 1121
4 D 415161 4151

How would I go about doing this in R

I've tried using:

df$columnd = substr(columnc, 1, 4)

This doesn't seem to work. The other thing is that I want it not to be attached to the same dataset. I want it all to be attached to a different variable. I'm not sure if that made any sense.

答案1

得分: 1

在您的代码中的更正:

添加 df$ 这里

df$columnd <- substr(df$columnc, 1, 4)

英文:

Correction in your code:

add df$there

df$columnd <- substr(df$columnc, 1, 4)

huangapple
  • 本文由 发表于 2023年2月7日 02:22:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365184.html
匿名

发表评论

匿名网友

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

确定