r function for counting specific row of 2 columns [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 1 year ago.
Improve this question
I need help how to compare 2 columns in r studio but only in the first 5 observations
code used first was
damgeByage<-table(test$Vehicle_Age,test$Vehicle_Damage)
damgeByage
which give me all 186 observation but only want the first 10 oobservation of both columns
I

We can subset the data with row index
table(test[1:10, c("Vehicle_Age", "Vehicle_Damage")])

akrun's answer works or you can also use:
table(test$Vehicle_Age[1:10],test$Vehicle_Damage[1:10])
the table is also subsettable if you just wanted the first 10 rows of the results:
damgeByage<-table(test$Vehicle_Age,test$Vehicle_Damage)
damgeByage[1:10,]

Related

undefined columns selected problem when trying to run this code [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 2 years ago.
Improve this question
I am trying to use the air dataset from the dummies package.
I tried:
library(dummies)
dumair<-air[c(5:18)]
but that throws the following error:
Error in `[.data.frame`(air, c(5:18)) : undefined columns selected
How can I overcome this?
You need to specify which columns you want to select. When selecting rows and columns from a dataframe, you need to specify them like this:
df[rows, columns]
If you need all columns, leave the "columns" field empty. Same if you want all rows but only some columns!
I believe you want to select all columns, but only the rows from 5 to 18, right?
So doing:
dumair <- air[c(5:18), ]
Should work!

Use of Ifelse and Or 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 4 years ago.
Improve this question
I have a dataset named SPT which has one column which consist 4 unique values-A,B,C,D, Now I have to create one more column which consist of 1 if first column had A, B otherwise 0.How to do it using ifelse command
Using ifelse:
SPT$new <- ifelse(SPT$col %in% c('A','B'),1,0)
Where new must be changed by the name of your new variable, and col by the name of the column that is storing the 4 letters

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?

Fetch a column value depending on a row number [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 the following df
Case Points
A 2
B 3
C 8
I would the like to fetch the Points of a certain row. So lets say I have a list with rownumbers 2 and 3
rownumbers <- c(2,3)
Then I would like to the fetch the numbers 3 and 8. Could anybody tell me how to do this?
Try:
mydata[rownumbers,"Points"]
This answered in comments so I made it as community wiki

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