2016-04-12 4 views
1

Ich bin neu in R und benötige Hilfe zum Umwandeln von Zeichenwerten in numerische Werte.Ändern des Zeichenwerts in numerisch für Variablensatz

I 19 Variablen aus einer CSV Datei lese und sie enthalten folgende Werte:

EXTREMELY IMPORTANT-10 ----> This should be actually value 10 
9 
8 
and so on 
NOT AT ALL IMPORTANT-01 ----> This should be actually value 01. 

Jetzt muss ich Werte all dieser 19 Variablen aus Charakter, den ich geschrieben habe folgenden Code in numerische ändern, und es ist gib mir einen Fehler.

path <- "C:/Rajesh-Pandit/SPSS-Trial/BANKING/Salary-Account" 
setwd(path) 

library(foreign) 

final_data <- read.csv("Rajhi-R.csv",header = TRUE, sep = ",") 
str(final_data) 

x <- (as.data.frame(final_data[,c(15:33)])) 

summary(x) 

y <- as.matrix(19) 
importance <- as.matrix(19) 

for(i in (1:19)){ 
    if (x[i] == "EXTREMELY IMPORTANT-10") {y[i] <- 10} 
    else if (x[i] == "NOT AT ALL IMPORTANT-01") {y[i] <- 1} 
    else if (x[i] == "02") {y[i] <- 2} 
    else if (x[i] == "03") {y[i] <- 3} 
    else if (x[i] == "04") {y[i] <- 4} 
    else if (x[i] == "05") {y[i] <- 5} 
    else if (x[i] == "06") {y[i] <- 6} 
    else if (x[i] == "07") {y[i] <- 7} 
    else if (x[i] == "08") {y[i] <- 8} 
    else if (x[i] == "09") {y[i] <- 9} 
} 
y 

importance <- y 

Grundsätzlich x[i] Zeichenwerte ändern sich nicht auf y[i]. Bitte geben Sie mir Feedback, warum und in diesem Fall nicht funktionieren.

Antwort

0

Sie können dies versuchen.

library('dplyr') 
library('purrr') 
library('magrittr') 
# use map_if() if you want to round all numeric columns/vectors into 2 decimals 
x %<>% mutate_each(funs(as.numeric)) %>% map_if(is.numeric, round, 2) 
Verwandte Themen