Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在不必输入每个元素的情况下缩短此向量?_R - Fatal编程技术网

R 如何在不必输入每个元素的情况下缩短此向量?

R 如何在不必输入每个元素的情况下缩短此向量?,r,R,如何在不必键入每个元素的情况下缩短此向量?我想在条形图上使用这些标签,但我发现代码太长了。有没有简单而简短的方法?谢谢 我尝试了粘贴(“1”,字母[1:6],sep=”“),但正如你所看到的,我不知道如何延长它 c("1a", "1b", "1c", "1d", "1e", "1f", "2a" ,"2b", "2c" , "2d", "2e", "2f", "3a", "3b", "3c", "3d", "3e", "3f", "3g", "4a", "4b", "4c", "4d" , "

如何在不必键入每个元素的情况下缩短此向量?我想在条形图上使用这些标签,但我发现代码太长了。有没有简单而简短的方法?谢谢 我尝试了粘贴(“1”,字母[1:6],sep=”“),但正如你所看到的,我不知道如何延长它

c("1a", "1b", "1c", "1d", "1e", "1f", "2a" ,"2b", "2c"  , "2d", "2e", "2f", "3a", "3b", "3c", "3d", "3e", "3f", "3g", "4a", "4b", "4c", "4d" , "4e", "4f", "4g", "5a", "5b", "5c", "5d")

对于可变长度的字母,您可以执行以下操作

l_len <- c(6, 6, 7, 7, 4)
num <- rep(seq_along(l_len), l_len)
let <- unlist(lapply(l_len, function(i) letters[seq_len(i)]))

paste0(num, let)
# [1] "1a" "1b" "1c" "1d" "1e" "1f" "2a" "2b" "2c" "2d" "2e" "2f" "3a" "3b" "3c" "3d" "3e" "3f" "3g" "4a" "4b" "4c" "4d" "4e" "4f" "4g" "5a" "5b" "5c" "5d"

paste0
paste(…,sep=”“)
的缩写,
rep
每次重复参数
1:5
6次,
letters[1:6]
自动重复,以便长度与
rep(1:5,each=6)
的长度匹配

l_len <- c(6, 6, 7, 7, 4)
num <- rep(seq_along(l_len), l_len)
let <- unlist(lapply(l_len, function(i) letters[seq_len(i)]))

paste0(num, let)
# [1] "1a" "1b" "1c" "1d" "1e" "1f" "2a" "2b" "2c" "2d" "2e" "2f" "3a" "3b" "3c" "3d" "3e" "3f" "3g" "4a" "4b" "4c" "4d" "4e" "4f" "4g" "5a" "5b" "5c" "5d"

paste0
paste(…,sep=”“)
的缩写,
rep
每次重复参数
1:5
6次,
字母[1:6]
自动重复,以便长度与
rep(1:5,每个=6)
的长度相匹配

fun <- function(x, y) paste0(x, y)
c(t(outer(1:5, letters[1:6], Vectorize(fun))))
#[1] "1a" "1b" "1c" "1d" "1e" "1f" "2a" "2b" "2c" "2d" "2e" "2f" "3a" "3b" .....

fun这里是使用
outer的另一种方法

fun <- function(x, y) paste0(x, y)
c(t(outer(1:5, letters[1:6], Vectorize(fun))))
#[1] "1a" "1b" "1c" "1d" "1e" "1f" "2a" "2b" "2c" "2d" "2e" "2f" "3a" "3b" .....

fun您可以使用
rep
sequence
生成所需的向量:

x  <- c(6,6,7,7,4)
paste0(rep(seq_along(x), x), letters[sequence(x)])
# [1] "1a" "1b" "1c" "1d" "1e" "1f" "2a" "2b" "2c" "2d" "2e" "2f" "3a" "3b" "3c"
#[16] "3d" "3e" "3f" "3g" "4a" "4b" "4c" "4d" "4e" "4f" "4g" "5a" "5b" "5c" "5d"

x您可以使用
rep
sequence
生成所需的向量:

x  <- c(6,6,7,7,4)
paste0(rep(seq_along(x), x), letters[sequence(x)])
# [1] "1a" "1b" "1c" "1d" "1e" "1f" "2a" "2b" "2c" "2d" "2e" "2f" "3a" "3b" "3c"
#[16] "3d" "3e" "3f" "3g" "4a" "4b" "4c" "4d" "4e" "4f" "4g" "5a" "5b" "5c" "5d"

x作为避免
字母
的替代方案,如何利用十进制到十六进制的转换

unlist(Map(function(i) sprintf("%x", 16 * i + 10:15), 1:5))
#[1] "1a" "1b" "1c" "1d" "1e" "1f" "2a" "2b" "2c" "2d" "2e" "2f" "3a" "3b" "3c"
#[16] "3d" "3e" "3f" "4a" "4b" "4c" "4d" "4e" "4f" "5a" "5b" "5c" "5d" "5e" "5f"

标志
“%x”
将十进制整数打印为十六进制整数;这组元素对应于从1到4的数字
16*i+10
16*i+11
,…,
16*i+15

作为避免字母
的替代方法,使用十进制到十六进制的转换怎么样

unlist(Map(function(i) sprintf("%x", 16 * i + 10:15), 1:5))
#[1] "1a" "1b" "1c" "1d" "1e" "1f" "2a" "2b" "2c" "2d" "2e" "2f" "3a" "3b" "3c"
#[16] "3d" "3e" "3f" "4a" "4b" "4c" "4d" "4e" "4f" "5a" "5b" "5c" "5d" "5e" "5f"

标志
“%x”
将十进制整数打印为十六进制整数;这组元素对应于从1到4的数字
16*i+10
16*i+11
,…,
16*i+15

嘿,伙计们。非常感谢你的帮助。但向量中的数字并不相等。使用这里的输入,这是我可以修改代码的最远距离。但是字母和数字不一致。记住,这些是简单条形图的标签,所以它们必须是正确的数字/字母:paste0(rep(1:5,times=c(6,6,7,7,4)),rep(字母[1:5],5])嘿,伙计们。非常感谢你的帮助。但向量中的数字并不相等。使用这里的输入,这是我可以修改代码的最远距离。但是字母和数字不一致。请记住,这些是简单条形图的标签,因此它们必须是正确的数字/字母:paste0(rep(1:5,times=c(6,6,7,7,4)),rep(字母[1:5],5]),感谢您的代码!它就像一个符咒…我很高兴。谢谢@GKi!:)谢谢你的代码!它就像一个符咒…我很高兴。谢谢@GKi!:)