Rounding all numbers in r with no decimals [closed] - r

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm just starting out in R and I'm trying to round all the numbers with no decimals in my data frame, preferably when I'm reading in a file.
I've looked up several ways to round decimals, but it is not clear to me where to place the code and what exactly to put in.
Could someone explain to me how to do this in the simplest way possible?
I'm working through an R script.

Where df is the name of the data.frame, and the 0 in the round function is number of decimals:
df <- data.frame(lapply(df, function(x){ if(is.numeric(x)){round(x, 0)}else{x}}))
Data used:
df <- data.frame(x1 = c(84.2,105.2,79.2,140.2,108.2,79.2,112.2,118.2,114.2,92.2),
x2 = c(138.3,110.3,84.3,45.3,128.3,99.3,100.3,124.3,121.3,115.3),
x3 = as.character(c(138.3,110.3,84.3,45.3,128.3,99.3,100.3,124.3,121.3,115.3)),
stringsAsFactors = FALSE)

Nevermind, found a simple function round_half_up() which solved the problem!

Related

Dataframe convert factors to numerical error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to convert factors from a data-frame to numeric using the commands
data[] <- lapply (data, function(x) as.numeric(as.character(x))
But it keeps asking me for more coding. What am I doing wrong?
The data-frame is named data and it consists of 50 rows and 2 columns. Will this command change every variable in numeric right? Or shall I do something else?
screenshot after using 'dput' at http://imgur.com/Sde9QSk.png
Shouldn't you add ) at the end of your code?

Can I group * things from this data? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I got data with ASCII form.
I ran it with R, and these data have * marked when it is under other condition.
enter image description here
V1, V2, V3, V4, V5 don't mean anything different. All that matters is to classify between *-ed things.
I tried c(V1,V2,V3,V4,V5) but it returns only the levels.
I have no idea. Help me with it.
Question. Can I specify *-ed things via some code?
Is there a way to make these columned things in one data?
Select the values marked with *. I guess these values come with the symbol from the original file, right?
In this case use:
position <- grep('\\*', as.matrix(distress[]))
selectedValues <- as.matrix(distress[])[position]
numericValues <- as.numeric(gsub('\*', '', selectedValues))

r unsplit to get original dataframe back [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have question about unsplit in R. Really appreciate if you could help.
I splitted a dataframe into smaller dataframes, by two factors.
mydf.list=split(df.original,list(factor1,factor2))
How do I use unsplit to get my dataframe back? I tried following, but didn't work.
df.updated=unsplit(mydf.list,list(factor1,factor2))
Thanks a lot.
I think this is what you are looking for. This example is based on the mtcars dataset, and #thelatemail's comment
data(mtcars) #load dataset
mydf.list<-split(mtcars,list("cyl","vs")) #split the dataset
unsplit(split(mydf.list, list("cyl", "vs") ), list("cyl", "vs")) #rejoin the dataset

Formatting data in R? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How do I format dates in R? I had to change something in my data set to account for blanks, and now my dates are very large negative numbers. I need to change them back into dates.
Assuming you have \t delimited input file.
Use the as.is argument to stop the read.table() function from converting the input variables. Then perhaps convert the date into something usable using strptime()
data <- read.table(file="...", sep="\t", as.is = TRUE)
data[,1] <- strptime(data[,1], "%Y-%m-%d")

apply function doesn't work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a data frame that has some empty entries. I set the
options(stringsAsFactors = FALSE)
so that I can change the empty cells. I then wrote the following code:
apply(my_data[,6:65],2, function(x) x[which(x=='')]<-0)
, hoping that it replaces all the empty cells with zeros. But it isn't working!
Note that my_data has 65 columns and columns 1:5 contain string.
Thanks in advance
No need to use apply, just use [<- with logical indexing
my_data[my_data==""] <- 0

Resources