小编Pik*_*tja的帖子

当df还包含字符串时,将data.frame转换为数字矩阵的正确方法是什么?

我有一个.csv文件中的数据框,其中包含数字和字符值.我想将此数据帧转换为矩阵.所有包含信息的都是数字(我删除的非数字行),因此应该可以将数据帧转换为数字矩阵.但是,我确实得到了一个字符矩阵.

我发现解决这个问题的唯一方法是使用as.numeric每一行,但这非常耗时.我很确定有一种方法可以用某种形式来做到这一点if(i in 1:n),但我无法弄清楚它是如何工作的.或者是真正开始使用数值的唯一方法,如此处提出的(制作矩阵数字和名称顺序)?

对大多数人来说,这可能是一件非常容易的事:P

矩阵要大很多,这只是前几行......这是代码:

cbind(
as.numeric(SFI.Matrix[ ,1]),
as.numeric(SFI.Matrix[ ,2]),
as.numeric(SFI.Matrix[ ,3]),
as.numeric(SFI.Matrix[ ,4]),
as.numeric(SFI.Matrix[ ,5]),
as.numeric(SFI.Matrix[ ,6]))  

# to get something like this again:

Social.Assistance Danger.Poverty GINI S80S20 Low.Edu        Unemployment 
0.147             0.125          0.34    5.5   0.149        0.135 0.18683691
0.258             0.229          0.27    3.8   0.211        0.175 0.22329362
0.207             0.119          0.22    3.1   0.139        0.163 0.07170422
0.219             0.166          0.25    3.6   0.114        0.163 0.03638525
0.278             0.218          0.29    4.1   0.270        0.198 0.27407825
0.288             0.204          0.26    3.6   0.303        0.211 0.22372633 …
Run Code Online (Sandbox Code Playgroud)

if-statement r numeric matrix

49
推荐指数
3
解决办法
13万
查看次数

R: horizontal barplot with y-axis-labels next to every bar

I want to design a barplot with 36 groups of 3 horizontal bars. Next to each group of 3, there should be one label.

我的代码很混乱(我第一次使用R),所以我希望它能用于一些虚拟数据......

无论如何:

Transcomp <- matrix(nrow=3, ncol=36)     # matrix
colnamesbarplot <- colnames(transComp)   # should be used as barplot labels
barplot <- 
barplot(transComp, 
    space=c(0,2),
    legend.text=TRUE,
    beside=TRUE,
    horiz=TRUE,
    density=NA,
    col=c("red1","red4","green3"),
    xlab="crimes per 100,000 inhabitants",
    ylab="districts and years",
    axes=TRUE
            )
Run Code Online (Sandbox Code Playgroud)

我找不到允许我直接在条形图旁边显示列名称的参数(我不在乎它们是在条形图的左侧还是右侧)...问题可能是数字酒吧绘制?

在answeres 添加文本中的R水平barplot,y轴在不同尺度?在barplot()标记轴标签为每个栏和各组条形图与回避团体不要误会我,我想获得...

感谢您的任何帮助!

plot label axis r

7
推荐指数
1
解决办法
2万
查看次数

R:如何将数据帧值缩短为第一个字符

我想缩短data.frame的一列值.现在,每个值都包含许多字母,例如

df$col1
[1] AHG    ALK    OPH   BCZ   LKH    QRQ    AAA   VYY
Run Code Online (Sandbox Code Playgroud)

我需要的只是第一封信:

df$col1
[1] A    A    O   B   L    Q    A   V
Run Code Online (Sandbox Code Playgroud)

我已阅读,建议使用其他条目gsub,stri_replace_all_charclassstrsplit.但我担心我需要帮助来实现这一点.

string r character-encoding gsub strsplit

6
推荐指数
1
解决办法
5460
查看次数

标签 统计

r ×3

axis ×1

character-encoding ×1

gsub ×1

if-statement ×1

label ×1

matrix ×1

numeric ×1

plot ×1

string ×1

strsplit ×1