R语言数据框拆分列:将一列拆成两列
你可以使用substr()函数将字符串拆分成两列。下面是一个例子:
# 创建一个示例数据框
df <- data.frame(col = c("abcde", "fghij", "klmno"))
# 拆分列
df$new_col1 <- substr(df$col, 1, 4)
df$new_col2 <- substr(df$col, 5, 5)
# 查看结果
df
输出结果如下:
col new_col1 new_col2
1 abcde ab e
2 fghij fg j
3 klmno kl o
在上述代码中,我们首先使用substr()函数将数据框df的col列的前四个字符提取到新的列new_col1中,然后使用同样的函数将col列的第五个字符提取到新的列new_col2中。
原文地址: https://www.cveoy.top/t/topic/qiJj 著作权归作者所有。请勿转载和采集!