trouble transforming variables numeric

I worked on my Data in SPSS and saved it as csv. In SPSS all variables are type numeric. Now I am working on it in R and there are some variables that are somehow in the wrong format.

mean(PISA$MATH, na.rm = TRUE)
[1] NA
Warning message:
In mean.default(PISA$MATH, na.rm = TRUE) :
Argument ist weder numerisch noch boolesch: gebe NA zurück
It says it`s not numeric.

However if I use the as.numeric function I loose a huge amount of data:

Warning message:
NAs introduced by coercion

Is there an option to transform the data to numeric without introducing NAs? Or does anyone know if I made a mistake in the process from SPSS to R?

@F_user
Hi there,
Did you load your original data into R from a CSV file OR a PASS file?
Would you like to provide a snippet of your code here to show us how you loaded your source data into R?

1 Like

I loaded a CSV file into r:

PISA <- read.table("PISA.csv", header=TRUE, sep=";", dec=".")

Hi @F_user, for put all columns as numeric you could try in this form:

data <- read.csv("yourfile.csv", stringsAsFactors = FALSE)

data <- sapply(data, as.numeric)

But as.numeric() return NA for any values that cannot be converted to numeric, such as character strings.
To replace NA values with zero, you can use the is.na().

data[is.na(data)] <- 0
1 Like

Thank you.

I tried it and I get the error " more column than colums names"

Well, but for get a better help try to put reproducible example for better understand the situation

Reprex

Othe way is paste the result of dput():

data <- read.csv("yourfile.csv", stringsAsFactors = FALSE) #load data
dput(head(data,20)) # For get all columns and the firs 20 rows of the data
1 Like

if you were to use the haven package , you could presumably directly read your SPSS file, without bothering with all the issues csv's can introduce.
Import and Export SPSS, Stata and SAS Files • haven (tidyverse.org)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.