我想简化下面的代码,以便它可以使用一个函数在两个数据帧之间循环,以创建子字符串并添加列。
usatrade_total$HTS <- (substr(usatrade_total$HTS10, 1,8))
usatrade_total$HTS6 <- (substr(usatrade_total$HTS10, 1,6))
usatrade_total$HTS4 <- (substr(usatrade_total$HTS10, 1,4))
usatrade_total$HTS2 <- (substr(usatrade_total$HTS10, 1,2))
worldtrade_total$HTS <- (substr(worldtrade_total$HTS10, 1,8))
worldtrade_total$HTS6 <- (substr(worldtrade_total$HTS10, 1,6))
worldtrade_total$HTS4 <- (substr(worldtrade_total$HTS10, 1,4))
worldtrade_total$HTS2 <- (substr(worldtrade_total$HTS10, 1,2))