Re-labeling columns in R [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 5 years ago.
Improve this question
I need to replace number values with something that makes more sense to the reader.
Value “-0.95197” represents “18-24” age group in the given table.
What would be the best way to go about this for all columns?

Your question is a little unclear. Are you trying to rename the header of each column in a data.frame? If so, try this, where df is the name of your data.frame. You need one piece of text for each column.
names(df) <- c("your", "header", "names", "go", "here")
If this is not what you want, then you need to provide more info.

Related

subset R undefined columns [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
Not sure what exactly the error is here I'd appreciate some insight. My code is as follows, and I checked the structure of the df rbbq, there is definitely a column called 'Shrimp' in it.
bbq1 = read.table('c:/Users/***/Documents/bbq.txt', sep=' ',
header=T)
bbq2 = read.table('c:/Users/***/Documents/bbqshrimp.txt', sep=' ',
header=T)
rbbq = merge(bbq1, bbq2, by='City')
finalbbq = subset(rbbq, rbbq$Shrimp=="Yes", select=c('City', 'State' ))
Error in `[.data.frame`(x, r, vars, drop = drop) :
undefined columns selected
I would use dplyr however that's not how the professor wants us to accomplish this. I am just trying to pull out the city and state of the locations which have, "Yes" for the Shrimp variable. Thanks for any help! This question is specific to the subset function, and not just a matter of calling up the specific lines.
EDIT: Final workaround was to assign my dataframe to another name, and that did the trick.
Remove the rbbq$from rbbq$Shrimp. Also, pretty sure city and state don't need to be quoted.

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))

Delete multiple columns whose names share the same suffix [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
I wanted to delete all the columns with "No", such as DIGSANo, SETFANo, ... How do I do it? Thank you!
The data is here
enter link description here
One way could be with dplyr and the functions that work inside select:
iris %>% select(-ends_with("Width"))
In you case you should do something like:
yourData %>% select(-ends_with("No"))

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