![](/img/trans.png)
[英]How to convert string to numeric values in R using publicly available data
[英]How to convert from string to numeric value in R
我正在尋找在R中一群人的身高的圖表
B / ca高度(例如5-11)以字符串形式出現,我想知道是否有任何技巧可以轉換為數字,以便將其繪制出來。
我假設您的數據是一個因素。 如果正確,則可以將所有級別編輯為數值,然后將因子轉換為數值向量。
levels(data$heights) #Levels of the factor
levels(data$heights)<-c(5*12+10,5*12+11,6*12,6*12+1) #Renaming factors in numerical make sure
##the numbers are in the same order as in your levels
data$heights<-as.numeric(levels(data$heights))[data$heights] #Changing factor GPA to numeric vector GPA
data$heights
提醒您,如果您的R將您的數據作為要素讀取,則此方法有效
require(stringr)
Split <- str_split(x, "-")
sapply(Split, function(x) x[1] + (x[2] / 11))
它也可以與基本R函數一起使用:
sapply(strsplit(x, "-"), function(x) {x <- as.numeric(x); x[1] + x[2] / 12})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.