英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论