根据现有变量的前 7 个字符创建一个新变量 [英] Create a new variable based on the first 7 characters of existing variable

查看:18
本文介绍了根据现有变量的前 7 个字符创建一个新变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力创建一个仅包含现有变量的前七个字符的新变量.对不起,如果这很明显,但我一直在努力寻找满足我在 Google/Stack Overflow 上需求的答案.

I've been struggling to create a new variable which contains only the first seven characters of an existing variable. Sorry if this if obvious but I've struggled to find an answer which meets my needs on Google/Stack Overflow.

我的数据如下所示:

variable
UKM5001028
UKM5001028
UKM5001028
UKM5001028

我想要:

variable2
UKM5001
UKM5001
UKM5001
UKM5001

我尝试了下面的代码并成功修剪了第 7 个字符,但是当我想创建一个新变量时,它尝试打印整个输出:

I tried the below code and succeeded in trimming at the 7th character, but it tried to print the whole output, when I want to make a new variable:

variable2<-mydata$variable
substr(variable2, 1, 7)

推荐答案

您向 substr

试试

variable2 <- substr(mydata$variable,1,7)

这篇关于根据现有变量的前 7 个字符创建一个新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆