R: Extract list element but without index number in output - r

This seems to be a beginner question but I couldn't find the answer anywhere. I know how to extract an element from a list using listname[[1]] but output will always include the index number like
[1] First element of the list
Same is true for using the name like listname$name or unlist(listname$name). All I want is
First element of the list
I could of course remove the index number using regex but I doubt that this is the way it should be :-)

The reason that the [1] appears is because all atomic types in R are vectors (of type character, numeric, etc), i.e. in your case a vector of length one.
If you want to see the output without the [1] the simples way is to cat the object:
> listname <- list("This is the first list element",
"This is the second list element")
> cat(listname[[1]], "\n")
This is the first list element
>

Related

Is there any way to detect a specific word and replace a whole string with another in R language?

I want to replace char value in R with another, it should be like this:
The key word to work is "Texas". And there is any way that I can do it with multiple files at once.
I have tried like this: df[df=="121 Texas"]<-"Texas" but it's clearly not smart at all when it comes to work with many dataframe at the same time.
Let's say you had a dataframe with a column named "Original" and you wanted to replae all the entries in that column that contained the letters "Texas" as a substring with just the character value "Texas":
df$Original[ grepl("Texas", df$Original) ] <- "Texas"
The grepl is one of the regex functions. It returns a logical value. The "[" function on the LHS of "<-" (technically the "[<-" function) can accept a logical index and only do the replacements for those items that register TRUE in the grepl test.

read in csv file in R and make a list out of last column

Content of my.csv
project names,task names
Build Finances,Calculate Earnings
Build Roads,Calculate Equipment Costs
Buy Food, Calculate Grocery Costs
The code I'm using to read /tmp/my.csv into a variable/vector is:
taskNamesAndprojectNames <- read.csv("/tmp/my.csv", header=TRUE)
What I want to do is to grab the last column of my.csv file which has been put into the csvContent variable. And then make a list out of it.
So, something like this:
#!/usr/bin/Rscript
taskNamesAndprojectNames <- read.csv("/tmp/my.csv", header=TRUE)
#str(tasklists)
#tasklists
#tasklists[,ncol(tasklists)]
taskNames <- list(taskNamesAndprojectNames[,-1])
typeof(taskNames)
length(taskNames)
The problem with the above code is, when i run length on the taskNames variable/vector to confirm that it has the correct number of items or elements, I only get a response of 1. Which is not accurate.
[roywell#test01 data]$ ~/readincsv.r
[1] "list"
[1] 1
What am I doing wrong here? Can someone help me correct this code? What i want to do is grab the last column of an excel csv sheet, get the values in that last column and put them into a variable. Make a list out of it. Then iterate through the list to confirm that values/input provided by a user matches at least one of the elements in the list.
taskNames <- list(taskNamesAndprojectNames[,-1]) makes a list with one element that is a character vector of length 3.
It sounds like you are looking for a vector in this case:
taskNames <- taskNamesAndprojectNames[,-1]
typeof(taskNames)
[1] "character"
length(taskNames)
[1] 3

named Element-wise operations in R

I am a beginner in R and apologize in advance for asking a basic question, but I couldn't find answer anywhere on Google (maybe because the question is so basic that I didn't even know how to correctly search for it.. :D)
So if I do the following in R:
v = c(50, 25)
names(v) = c("First", "Last")
v["First"]/v["Last"]
I get the output as:
First
2
Why is it that the name, "First" appears in the output and how to get rid of it?
From help("Extract"), this is because
Subsetting (except by an empty index) will drop all attributes except names, dim and dimnames.
and
The usual form of indexing is [. [[ can be used to select a single element dropping names, whereas [ keeps them, e.g., in c(abc = 123)[1].
Since we are selecting single elements, you can switch to double-bracket indexing [[ and names will be dropped.
v[["First"]] / v[["Last"]]
# [1] 2
As for which name is preserved when using single bracket indexing, it looks like it's always the first (at least with the / operator). We'd have to go digging into the C source for further explanation. If we switch the order, we still get the first name on the result.
v["Last"] / v["First"]
# Last
# 0.5

Using strsplit ON a list in R [duplicate]

I'm just learning R and having a hard time wrapping my head around how to extract elements from objects in a list. I've parsed a json file into R giving me list object. But I can't figure out how, from there, to extract various json elements from the list. here's a truncated look at how my data appears after parsing the json:
> #Parse data into R objects#
> list.Json= fromJSON(,final.name, method = "C")
> head(listJson,6)
[[1]]
[[1]]$contributors
NULL
[[1]]$favorited
[1] FALSE
...[truncating]...
[[5]]
[[5]]$contributors
NULL
[[5]]$favorited
[1] FALSE
I can figure out how to extract the favorites data for one of the objects in the list
> first.object=listJson[1]
> ff=first.object[[1]]$favorited
> ff
[1] FALSE
But I'm very confused about how to extract favorited for all objects in the list. I've looked into sappily, is that the correct approach? Do I need to put the above code into a for...next loop?
sapply is going to apply some function to every element in your list. In your case, you want to access each element in a (nested) list. sapply is certainly capable of this. For instance, if you want to access the first child of every element in your list:
sapply(listJson, "[[", 1)
Or if you wanted to access the item named "favorited", you could use:
sapply(listJson, "[[", "favorited")
Note that the [ operator will take a subset of the list you're working with. So when you access myList[1], you still have a list, it's just of length 1. However, if you reference myList[[1]], you'll get the contents of the first space in your list (which may or may not be another list). Thus, you'll use the [[ operator in sapply, because you want to get down to the contents of the list.

in R, extract part of object from list

I'm just learning R and having a hard time wrapping my head around how to extract elements from objects in a list. I've parsed a json file into R giving me list object. But I can't figure out how, from there, to extract various json elements from the list. here's a truncated look at how my data appears after parsing the json:
> #Parse data into R objects#
> list.Json= fromJSON(,final.name, method = "C")
> head(listJson,6)
[[1]]
[[1]]$contributors
NULL
[[1]]$favorited
[1] FALSE
...[truncating]...
[[5]]
[[5]]$contributors
NULL
[[5]]$favorited
[1] FALSE
I can figure out how to extract the favorites data for one of the objects in the list
> first.object=listJson[1]
> ff=first.object[[1]]$favorited
> ff
[1] FALSE
But I'm very confused about how to extract favorited for all objects in the list. I've looked into sappily, is that the correct approach? Do I need to put the above code into a for...next loop?
sapply is going to apply some function to every element in your list. In your case, you want to access each element in a (nested) list. sapply is certainly capable of this. For instance, if you want to access the first child of every element in your list:
sapply(listJson, "[[", 1)
Or if you wanted to access the item named "favorited", you could use:
sapply(listJson, "[[", "favorited")
Note that the [ operator will take a subset of the list you're working with. So when you access myList[1], you still have a list, it's just of length 1. However, if you reference myList[[1]], you'll get the contents of the first space in your list (which may or may not be another list). Thus, you'll use the [[ operator in sapply, because you want to get down to the contents of the list.

Resources