R - subsetting a list [closed] - r

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am quite new to R and it would be a big help to find a way to do this.
I have a list of values(just one Column and around 16000 values) and I have to break this list up into smaller packets of 1000 values each. Then save each list as a csv file.
Is there a way of doing this is R?
Thank you in advance,
Dgupta

Something like this:
data <- as.data.frame(list)
groups <- split(1:nrow(data), ceiling(seq_along(1:nrow(data)/1000))
for (i in 1:length(groups)){write.csv(data[groups[1,],file=paste(i,'csv'))}

You can split the vector up by using a colon symbol in the vector index. For example:
> x <- c(10,20,30,40)
> x[1:2]
[1] 10 20
you can then write a vector to a csv by using the write.csv()function. One way to approach this is to write loop that gets 1000 items at a time from the vector and writes it to a csv.

Related

Create array with numeric data and characters [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm new to R. I would like to know how I can create an array with two dimensions, the first being a numeric matrix and the second a character matrix?
Thanks.
An array cant have multiple datatypes. What you are looking for is the dataframe.
For example, you can create a dataframe with a numeric and a character column like this:
df <- data.frame(numericColumn=1:26,characterColumn=LETTERS)
To access these columns, you can use square brackets or the names. For more details, just read the documentation of dataframe or search for some examples.
If you really want to store matrices of different types in one object, you can use a list, for example like
matrixList <- list(numericMatrix=someNumericMatrix,characterMatrix=someCharacterMatrix)

how to keep a large number of data.frame in .csv [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
There are a large number of data.frame (more than 50). How can I save them quickly in .csv?
write.csv()
50 lines of code, it's awful...
Help me, guys!
If I understand the many data.frames may be available in in your R session...
First create a vector with the names of the data.frames... use ls or some thing similar. Then use get to get the R object after the names (the data.frames in this case)
myfiles <- ls ()
Then
for (d in myfiles) {
current <- get (d)
write.csv (current, filename = paste0 (d, ".csv"))
}

R: Remove a row from all data.frames in global environment [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have over 200 data.frames in my global environment. I would like to remove the first row from each data.frame, but I am not sure how.
Any help will be appreciated please let me know if further information is needed.
This will list all the data frames in your environment, remove the first row from each, and organize them into a list of data frames. Generally, better practice to have them in a list so you can more easily apply functions across them and access them.
df <- lapply(ls(), function(x) get(x)[-1,])
Update: good idea to check if objects are in fact data frames and only work with those. First we create a logical vector listing dataframes, then combine them into a list and remove the first row of each.
dfs = sapply(ls(), is.data.frame)
lapply(mget(names(dfs)[dfs]), "[", -1, , drop = FALSE)
thanks to comments for finding my error and providing more efficient solutions

Advanced subsetting r data frame [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Without using a special function, can you do the following to change the values in an R dataframe from a column that have length greater than 4:
df[length(df$Column1)>4,"Column1"] = "replacement value"
This does not seem to work, is there an alternative index style I can use, or do I need to use a function?
Thanks
The function to determine the length of an entry, like a word in a dataframe, is nchar(), and not length(). The latter is typically used to determine the number of entries in a vector.
You could therefore try using:
df[nchar(df$Column1) > 4, "Column1"] <- "replacement value"

How to read first 1000 lines of .csv file into R? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have very big .csv file, it's around a few GB.
I want to read first few thousand lines of it.
Is there any method to do this efficiently?
Use the nrows argument in read.csv(...)
df <- read.csv(file="my.large.file.csv",nrows=2000)
There is also a skip= parameter that tells read.csv(...) how many lines to skip before you start reading.
If your file is that large you might be better off using fread(...) in the data.table package. Same arguments.
If you're on UNIX or OS/X, you can use the command line:
head -n 1000 myfile.csv > myfile.head.csv
Then just read it in R like normal.

Resources