这是一个非常基本的问题。如何将数据框的列名设置为列索引?因此,如果您有4列,则列名称将为if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
paint.setLetterSpacing(-0.04f); // setLetterSpacing is only available from LOLLIPOP and on
canvas.drawText(text, xOffset, yOffset, paint);
} else {
float spacePercentage = 0.05f;
drawKernedText(canvas, text, xOffset, yOffset, paint, spacePercentage);
}
/**
* Draw kerned text by drawing the text string character by character with a space in between.
* Return the width of the text.
* If canvas is null, the text won't be drawn, but the width will still be returned/
* kernPercentage determines the space between each letter. If it's 0, there will be no space between letters.
* Otherwise, there will be space between each letter. The value is a fraction of the width of a blank space.
*/
private int drawKernedText(Canvas canvas, String text, float xOffset, float yOffset, Paint paint, float kernPercentage) {
Rect textRect = new Rect();
int width = 0;
int space = Math.round(paint.measureText(" ") * kernPercentage);
for (int i = 0; i < text.length(); i++) {
if (canvas != null) {
canvas.drawText(String.valueOf(text.charAt(i)), xOffset, yOffset, paint);
}
int charWidth;
if (text.charAt(i) == ' ') {
charWidth = Math.round(paint.measureText(String.valueOf(text.charAt(i)))) + space;
} else {
paint.getTextBounds(text, i, i + 1, textRect);
charWidth = textRect.width() + space;
}
xOffset += charWidth;
width += charWidth;
}
return width;
}
。我使用的数据框最多可以有100列。
答案 0 :(得分:3)
使用以数字开头的名称命名列名称并不好。假设我们将其命名为seq_along(D)
。当我们尝试提取列时,它变得不必要地复杂化。例如,
names(D) <- seq_along(D)
D$1
#Error: unexpected numeric constant in "D$1"
在这种情况下,我们可能需要backticks
或""
D$"1"
#[1] 1 2 3
D$`1`
#[1] 1 2 3
但是,[
应该有效
D[["1"]]
#[1] 1 2 3
我会用
names(D) <- paste0("Col", seq_along(D))
D$Col1
#[1] 1 2 3
或者
D[["Col1"]]
#[1] 1 2 3
D <- data.frame(a=c(1,2,3),b=c(4,5,6),c=c(7,8,9),d=c(10,11,12))
答案 1 :(得分:1)
只需使用names
:
D <- data.frame(a=c(1,2,3),b=c(4,5,6),c=c(7,8,9),d=c(10,11,12))
names(D) <- 1:ncol(D) # sequence from 1 through the number of columns